PHP is_subclass_of() Function

What is PHP is_subclass_of() Function?

If you want to determine whether a class of an object or a class

  • is a sub class of a parent class
  • or, implements an interface,

then, use is_subclass_of() function.

Syntax:

is_subclass_of(object_or_class, class_or_interface, allow_string)

Parameters:

The Function has 2 required parameters and 1 optional parameter-

object_or_class (Required): It specifies an object or a class.

class_or_interface (Required): It specifies a parent class or interface of the 1st parameter.

allow_string(Optional): It specifies a boolean value(TRUE or FALSE) which controls 2 things-

  • TRUE – if it is TRUE (It is default), the first parameter can be either a class or an object. Also, if the class doesn’t exist, it will trigger the auto loader to load the class.
  • FALSE – if it is FALSE, the first parameter must be an object, meaning it doesn’t allow a string class. Also, the class prevent calling auto loader if the class in the 1st parameter doesn’t exist.

Return Values:

The function returns-

  • TRUE – if the 1st parameter is a sub class of 2nd parameter or the 2nd parameter is the interface of the 1st parameter.
  • FALSE – if the 1st parameter is not a sub class of 2nd parameter or the 2nd parameter is not the interface of the 1st parameter.

Examples:

Example:

<?php
class Human {
    var $hand = 2;
}
class Male extends Human {
    var $hormones = 'Testosterone';
}

$objMale = new Male();

if (is_subclass_of('Male', 'Human')) { echo "The class 'Male' is subclass of 'Human'.<br />"; }
else { echo "The class 'Male' is not subclass of 'Human'"; }

if (is_subclass_of($objMale, 'Human')) { echo "The class of the object '\$objMale' is a subclass of 'Human'.<br />"; }
else { echo "Male is not a subclass of Human"; }
?>

Output:

The class 'Male' is subclass of 'Human'.
The class of the object '$objMale' is a subclass of 'Human'.

Example 2:

<?php
interface Human {
    public function talk();
}
class Male implements Human {
    function talk() {
        echo "Hello, John";
    }
}

$objMale = new Male;

if (is_subclass_of('Male', 'Human')) { echo "class 'Male' implements interface 'Human'.<br />"; }
else { echo "Male doesn't implement interface 'Human'.<br />"; }

if (is_subclass_of($objMale, 'Human')) { echo "The class of the object '\$objMale' implements interface 'Human'.<br />"; }
else { echo "The class of the object \$objMale does't implement Human interface.<br />"; }

if (is_subclass_of($objMale, 'Animal')) { echo "The class 'Male' implements interface 'Animal'."; }
else { echo "The class 'Male' does't implement interface 'Animal'."; }
?>

Output:

class 'Male' implements interface 'Human'.
The class of the object '$objMale' implements interface 'Human'.
The class 'Male' doesn't implement interface 'Animal'.

Practical Usages of is_subclass_of() Function:

The function is a very powerful tool in object oriented programming to verify the interface, inheritance relationship.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP is_subclass_of() Function

is_subclass_of() is a class/object type function in PHP. Use this function to find out sub class of a parent class.

Reference:

https://www.php.net/manual/en/function.is-subclass-of.php