get_parent_class
Returns the name of the parent class of an object or class name, or false when there is no parent.
get_parent_class(object|string $object_or_class = ?): string|false- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$object_or_classoptional | object|string | An object instance or the name of a class whose parent you want. Omitting it is deprecated as of PHP 8.3. |
Return value
string|falseThe name of the parent class, or false if the object or class has no parent, or the named class does not exist.
Errors
Does not throw. A non-existent class name returns false, and calling it with no argument emits an E_DEPRECATED notice as of PHP 8.3.
Changelog
- PHP 8.3.0Calling get_parent_class() without an argument now emits E_DEPRECATED. Previously, inside a class it returned that class's parent.
- PHP 8.0.0The object_or_class parameter now accepts only objects or valid class names; other values raise a TypeError.
get_parent_class() walks exactly one step up the inheritance chain. Given class Puppy extends Dog extends Animal, calling it on a Puppy returns "Dog" — never "Animal". To reach the top of the chain you call it repeatedly, or use class_parents() from SPL, which returns every ancestor at once.
When to reach for it
- Building a lookup that falls back from a specific class to its parent — template resolvers, event handler maps, serializer registries.
- Writing debugging or logging output that shows where a class sits in a hierarchy.
- Inside a class, to call or reference the parent by name (though
parent::is almost always better).
What trips people up
It only reports classes. Interfaces a class implements and traits it uses are invisible to it; use class_implements() and class_uses() for those.
The name comes back with its original case as declared, but PHP class names are case-insensitive, so compare with strcasecmp() or use SomeClass::class and is_a() rather than a raw === against a hand-typed string. In a namespace, the returned name is fully qualified and has no leading backslash.
If you only want to know "is this thing a kind of X?", use instanceof or is_subclass_of() instead — those check the whole chain, which is nearly always what you actually mean.
Example
When to use it
- Resolving a template or view file by falling back from a model class to its parent class name.
- Printing a readable inheritance trace when logging an unexpected object type.
- Detecting whether a value object is a plain base type or a specialised subclass before serialising it.
Frequently asked questions
Does get_parent_class return all parent classes?
No — it returns only the immediate parent, one level up. Use class_parents() to get every ancestor at once, or call get_parent_class() in a loop.
What is the difference between get_parent_class and get_class?
get_class() returns the class of the object itself; get_parent_class() returns the class it extends. On an object with no parent, get_class() still returns a name while get_parent_class() returns false.
Why does get_parent_class() emit a deprecation notice?
Since PHP 8.3, calling it with no argument is deprecated. Always pass $this, another object, or a class name explicitly.
More examples
Walking the whole ancestry
The loop ends when get_parent_class() returns false, printing B then A. class_parents() gives the same ancestors in one call, so the loop is only worth writing when you need to stop early.
<?php
class A {}
class B extends A {}
class C extends B {}
$class = 'C';
while ($class = get_parent_class($class)) {
echo $class, "\n";
}
print_r(array_values(class_parents('C')));Interfaces and traits are not parents
Only "Base" comes back from get_parent_class(). The implemented interface and the used trait show up only through class_implements() and class_uses(), and a plain stdClass reports false because it has no parent.
<?php
interface Jsonish {}
trait Timestamps {}
class Base {}
class Doc extends Base implements Jsonish { use Timestamps; }
$d = new Doc();
echo get_parent_class($d), "\n";
print_r(array_values(class_implements($d)));
print_r(array_values(class_uses($d)));
var_dump(get_parent_class(new stdClass()));
Discussion