is_subclass_of
Description
The is_subclass_of of Class / Object for PHP checks if the object has this class as one of its parents or implements it.
Syntax
is_subclass_of(
mixed $object_or_class,
string $class,
bool $allow_string = true
): boolParameters
object_or_class
A class name or an object instance. No error is generated if the class does not exist.
class
The class name.
allow_string
If this parameter set to false, string class name as object_or_class is not allowed. This also prevents from calling autoloader if the class doesn't exist.
Return
This function returns true if the object object_or_class, belongs to a class which is a subclass of class, false otherwise.
Examples
1 · object_or_class class
<?
class myclassparent
{
}
class myclasschild extends myclassparent
{
}
$object_or_class = new myclasschild();
$class = "myclassparent";
$return = is_subclass_of($object_or_class, $class);
var_export($return);
true
2 · allow_string · false
<?
class myclassparent
{
}
class myclasschild extends myclassparent
{
}
$object_or_class = "myclasschild";
$class = "myclassparent";
$allow_string = false;
$return = is_subclass_of($object_or_class, $class, $allow_string);
var_export($return);
false
3 · allow_string · true
<?
class myclassparent
{
}
class myclasschild extends myclassparent
{
}
$object_or_class = "myclasschild";
$class = "myclassparent";
$allow_string = true;
$return = is_subclass_of($object_or_class, $class, $allow_string);
var_export($return);
true
4 · namespace
<?
namespace mynamespace;
class myclassparent
{
}
class myclasschild extends myclassparent
{
}
$object_or_class = new myclasschild();
$class = "mynamespace\myclassparent";
$return = is_subclass_of($object_or_class, $class);
var_export($return);
true
5 · interface
<?
interface myinterface
{
}
class myclass implements myinterface
{
}
$object_or_class = new myclass();
$class = "myinterface";
$return = is_subclass_of($object_or_class, $class);
var_export($return);
true