Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

class_parents

Description

The class_parents of SPL for PHP returns the parent classes of the given class.

Syntax

class_parents(
    object|string $object_or_class,
    bool $autoload = true
): array|false

Parameters

object_or_class

An object (class instance) or a string (class name).

autoload

Whether to autoload if not already loaded.

Return

Returns an array on success, or false when the given class doesn't exist.

Examples

1 · object_or_class · object

<?

class myclass1
{
}
class myclass2 extends myclass1
{
}

$object_or_class = new myclass2;

$return = class_parents($object_or_class);

print_r($return);

?>
Array
(
    [myclass1] => myclass1
)

2 · object_or_class · class

<?

class myclass1
{
}
class myclass2 extends myclass1
{
}

$object_or_class = "myclass2";

$return = class_parents($object_or_class);

print_r($return);

?>
Array
(
    [myclass1] => myclass1
)

3 · autoload · false

<?

spl_autoload_register();

class myclass1
{
}
class myclass2 extends myclass1
{
}

$object_or_class = "myclass2";
$autoload = false;

$return = class_parents($object_or_class, $autoload);

print_r($return);

?>
Array
(
    [myclass1] => myclass1
)

4 · autoload · true

<?

spl_autoload_register();

class myclass1
{
}
class myclass2 extends myclass1
{
}

$object_or_class = "myclass2";
$autoload = true;

$return = class_parents($object_or_class, $autoload);

print_r($return);

?>
Array
(
    [myclass1] => myclass1
)

5 · multiple

<?

class myclass1
{
}
class myclass2 extends myclass1
{
}
class myclass3 extends myclass2
{
}
class myclass4 extends myclass3
{
}

$object_or_class = "myclass4";

$return = class_parents($object_or_class);

print_r($return);

?>
Array
(
    [myclass3] => myclass3
    [myclass2] => myclass2
    [myclass1] => myclass1
)
HomeMenu