is_subclass_of() works also with classes between the class of obj and the superclass.
example:
<?php
class A {};
class B extends A {};
class C extends B {};
$foo=new C();
echo ((is_subclass_of($foo,'A')) ? 'true' : 'false');
?>
echoes 'true' .
(PHP 4, PHP 5, PHP 7, PHP 8)
is_subclass_of — Checks if the object has this class as one of its parents or implements it
Checks if the given object_or_class
has the class
class
as one of its parents or implements it.
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.
This function returns true
if the object object_or_class
,
belongs to a class which is a subclass of
class
, false
otherwise.
Example #1 is_subclass_of() example
<?php
// define a class
class WidgetFactory
{
var $oink = 'moo';
}
// define a child class
class WidgetFactory_Child extends WidgetFactory
{
var $oink = 'oink';
}
// create a new object
$WF = new WidgetFactory();
$WFC = new WidgetFactory_Child();
if (is_subclass_of($WFC, 'WidgetFactory')) {
echo "yes, \$WFC is a subclass of WidgetFactory\n";
} else {
echo "no, \$WFC is not a subclass of WidgetFactory\n";
}
if (is_subclass_of($WF, 'WidgetFactory')) {
echo "yes, \$WF is a subclass of WidgetFactory\n";
} else {
echo "no, \$WF is not a subclass of WidgetFactory\n";
}
if (is_subclass_of('WidgetFactory_Child', 'WidgetFactory')) {
echo "yes, WidgetFactory_Child is a subclass of WidgetFactory\n";
} else {
echo "no, WidgetFactory_Child is not a subclass of WidgetFactory\n";
}
?>
The above example will output:
yes, $WFC is a subclass of WidgetFactory no, $WF is not a subclass of WidgetFactory yes, WidgetFactory_Child is a subclass of WidgetFactory
Example #2 is_subclass_of() using interface example
<?php
// Define the Interface
interface MyInterface
{
public function MyFunction();
}
// Define the class implementation of the interface
class MyClass implements MyInterface
{
public function MyFunction()
{
return "MyClass Implements MyInterface!";
}
}
// Instantiate the object
$my_object = new MyClass;
// Works since 5.3.7
// Test using the object instance of the class
if (is_subclass_of($my_object, 'MyInterface')) {
echo "Yes, \$my_object is a subclass of MyInterface\n";
} else {
echo "No, \$my_object is not a subclass of MyInterface\n";
}
// Test using a string of the class name
if (is_subclass_of('MyClass', 'MyInterface')) {
echo "Yes, MyClass is a subclass of MyInterface\n";
} else {
echo "No, MyClass is not a subclass of MyInterface\n";
}
?>
The above example will output:
Yes, $my_object is a subclass of MyInterface Yes, MyClass is a subclass of MyInterface
Note:
Using this function will use any registered autoloaders if the class is not already known.
is_subclass_of() works also with classes between the class of obj and the superclass.
example:
<?php
class A {};
class B extends A {};
class C extends B {};
$foo=new C();
echo ((is_subclass_of($foo,'A')) ? 'true' : 'false');
?>
echoes 'true' .
This might be useful to someone, so:
If you're using Autoload, you should be aware that this will attempt to autoload $classname if it isn't already loaded. I discovered this when I had something using is_subclass_of inside an error thrown by autoload, which then recursed until it ran out of memory.
The $allow_string parameter is not very clearly documented. When true, it simply allows the first parameter to be the name of a class, instead of an object whose class we are interested in.
Some usage examples:
class parent_class {
// objects and methods ...
}
$possible_child_object = new possible_child_class(); // might be an extension of parent_class
$result = is_subclass_of($possible_child_object, 'parent_class'); // valid
$result = is_subclass_of($possible_child_object, 'parent_class', false); // valid
$result = is_subclass_of('possible_child_class', 'parent_class', true); // valid
$result = is_subclass_of('possible_child_class', 'parent_class', false); // not valid
You can check if your current class is a child with:
<?php is_subclass_of($this, self::class) ?>
<?php
class a {
public function check(): bool {
return is_subclass_of($this, self::class);
}
}
class b extends a {}
$a = new a();
$b = new b();
echo $a->check() ? 'Yes' : 'No';
echo PHP_EOL;
echo $b->check() ? 'Yes' : 'No';
echo PHP_EOL;
?>
prints:
No
Yes
<?php
interface I {
}
class A implements I {
}
class B extends A {
}
if (is_subclass_of('A', 'I')) {
echo 'good<br>';
}
else {
echo 'bad<br>';
}
if (is_subclass_of('B', 'I')) {
echo 'good<br>';
}
else {
echo 'bad<br>';
}
if (is_subclass_of('B', 'A')) {
echo 'good<br>';
}
else {
echo 'bad<br>';
}
?>
result:
bad <- you must to describe intermediate class B to be good
good
good
If you need something similar to is_subclass_of() to determine if a class implements an interface before instantiating it, use reflection:
<?php
interface A_Interface {}
class A implements A_Interface {}
$reflectionA = new ReflectionClass('A');
var_dump(
$reflectionA->implementsInterface('A_Interface')
);
?>
bool(true)
It would appear that is_subclass_of is case insensitive unlike get_class in php5.
i.e.
<?php
class fooBar {}
class bar extends fooBar {}
assert(get_class(new fooBar()) == "fooBar");
assert(is_subclass_of(new bar(), "foobar") == true);
?>
i run across this while migrating some code from php4 to php5 and the code would only half-the-time break.
For PHP4:
<?php
/** Returns whether specified class is subclass of the other class. */
function is_subclass($sClass, $sExpectedParentClass){
do if( $sExpectedParentClass === $sClass ) return true;
while( false != ($sClass = get_parent_class($sClass)) );
return false;
}
// Test:
class A {} class B extends A {} class C extends B {} echo (int) is_subclass('C', 'A');
?>