319 of 410 menu

The get_parent_class Function

The get_parent_class function returns the name of the parent class for the specified object or class. If no parent class exists, the function returns false. You can pass either an object or a class name as a string as a parameter.

Syntax

get_parent_class(object|string);

Example

Get the parent class for an object:

<?php class ParentClass {} class ChildClass extends ParentClass {} $obj = new ChildClass(); echo get_parent_class($obj); ?>

Execution result:

'ParentClass'

Example

Get the parent class by the class name:

<?php class ParentClass {} class ChildClass extends ParentClass {} echo get_parent_class('ChildClass'); ?>

Execution result:

'ParentClass'

Example

Try to get the parent class for a class without a parent:

<?php class SimpleClass {} var_dump(get_parent_class('SimpleClass')); ?>

Execution result:

false

See Also

byenru