is_subclass_of
Checks whether a class is a child of another class.
Syntax
is_subclass_of(mixed $object_or_class, string $class, bool $allow_string = true): boolParameters
| Parameter | Type | Description |
|---|---|---|
| $object_or_class | mixed | An object or a class name. |
| $class | string | The parent or interface name. |
| $allow_string | bool | Optional. Allow a class name string. |
Return value
bool`true` for a child; `false` for the class itself.
Examples
A child and the class itself:
<?php
var_dump(is_subclass_of('InvalidArgumentException', 'Exception'));
var_dump(is_subclass_of('Exception', 'Exception'));
var_dump(is_a('Exception', 'Exception', true));Output:
bool(true)
bool(false)
bool(true)Notes
Unlike is_a(), a class does not count as its own child.
See also
- is_a — Checks whether an object belongs to a class or one of its children.
- get_class — Returns the class name of an object.
- class_exists — Checks whether a class is defined.
In the tutorial
- Inheritance — Class inheritance in PHP — extends, protected, overriding methods, parent and polymorphism.