Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

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): bool

Parameters

ParameterTypeDescription
$object_or_classmixedAn object or a class name.
$classstringThe parent or interface name.
$allow_stringboolOptional. 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.

Thanks for reading!

Donate to the site — we work hard for you.