Menu Home

is_subclass_of

osbo.com | PHP | Functions | is_subclass_of

Checks if the object has this class as one of its parents or implements it

Syntax

is_subclass_of ( mixed $object , string $class_name [, bool $allow_string = TRUE ] ) : bool

Parameters

object

A class name or an object instance. No error is generated if the class does not exist.

class_name

The class name

allow_string

If this parameter set to false, string class name as object is not allowed. This also prevents from calling autoloader if the class doesn't exist.

Return

This function returns TRUE if the object object, belongs to a class which is a subclass of class_name, FALSE otherwise.

Examples

object class_name

<?

class myclassparent
{
}
class myclasschild extends myclassparent
{
}

$object = new myclasschild();
$class_name = "myclassparent";

$return = is_subclass_of($object, $class_name);

var_export($return);

?>
true

allow_string | false

<?

class myclassparent
{
}
class myclasschild extends myclassparent
{
}

$object = "myclasschild";
$class_name = "myclassparent";
$allow_string = false;

$return = is_subclass_of($object, $class_name, $allow_string);

var_export($return);

?>
false

allow_string | true

<?

class myclassparent
{
}
class myclasschild extends myclassparent
{
}

$object = "myclasschild";
$class_name = "myclassparent";
$allow_string = true;

$return = is_subclass_of($object, $class_name, $allow_string);

var_export($return);

?>
true

namespace

<?

namespace mynamespace;

class myclassparent
{
}
class myclasschild extends myclassparent
{
}

$object = new myclasschild();
$class_name = "mynamespace\myclassparent";

$return = is_subclass_of($object, $class_name);

var_export($return);

?>
true

interface

<?

interface myinterface
{
}

class myclass implements myinterface
{
}

$object = new myclass();
$class_name = "myinterface";

$return = is_subclass_of($object, $class_name);

var_export($return);

?>
true

PHP