The class_parents Function

The class_parents function returns an array with the names of all parent classes for the specified class or object. The first parameter accepts an object or class name, and the second (optional) one is the autoload flag.

Syntax

phpclass_parents(
  object|string $class,
  [bool $autoload = true]
): array|false

Example

Get the parent classes for an object:

php<?php
  class ParentClass {}
  class ChildClass extends ParentClass {}
  
  $obj = new ChildClass();
  $res = class_parents($obj);
  print_r($res);
?>

Execution result:

php['ParentClass' => 'ParentClass']

Example

Get the parent classes by class name:

php<?php
  class GrandParent {}
  class ParentClass extends GrandParent {}
  class ChildClass extends ParentClass {}
  
  $res = class_parents('ChildClass');
  print_r($res);
?>

Execution result:

php['ParentClass' => 'ParentClass', 'GrandParent' => 'GrandParent']

Example

Check the operation with a non-existent class:

php<?php
  $res = class_parents('NonExistentClass', false);
  var_dump($res);
?>

Execution result:

phpfalse

See Also

  • the class_implements function,
    which returns the interfaces of a class
  • the get_class function,
    which returns the class name of an object
  • the is_subclass_of function,
    which checks class inheritance