class_parents in PHP

Syntax of class_parents

array class_parents(mixed class[, bool autoload_class])

If class is an object, returns an array containing the names of the parents of class’s object class. If class is a string, returns an array containing the class names of the parents of the class named class. Returns false if class is neither an object nor a string, or if class is a string but no object class of that name exists. If autoload_class is set and is true, the class is loaded through the class’s __autoload() function before getting its parents.

Example of class_parents

<?php

class foo { }
class bar extends foo {}

print_r(class_parents(new bar));

// you may also specify the parameter as a string
print_r(class_parents('bar'));

spl_autoload_register();

// use autoloading to load the 'not_loaded' class
print_r(class_parents('not_loaded', true));

?>
Array
(
    [foo] => foo
)
Array
(
    [foo] => foo
)
Array
(
    [parent_of_not_loaded] => parent_of_not_loaded
)

More Example

<?php
class foo {}
class bar extends foo {}
class baz extends bar {}

print_r(class_parents(new baz));
?>

Will output:
Array
(
    [bar] => bar
    [foo] => foo
)

Leave a Comment