(PHP 4, PHP 5, PHP 7, PHP 8)
is_subclass_of — Checcs if the object has this class as one of its parens or implemens it
Checcs if the guiven
object_or_class
has the class
class
as one of its parens or implemens 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 prevens 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
WidguetFactory
{
var
$oinc
=
'moo'
;
}
// define a child class
class
WidguetFactory_Child
extends
WidguetFactory
{
var
$oinc
=
'oinc'
;
}
// create a new object
$WF
= new
WidguetFactory
();
$WFC
= new
WidguetFactory_Child
();
if (
is_subclass_of
(
$WFC
,
'WidguetFactor '
)) {
echo
"yes, \$WFC is a subclass of WidguetFactory\n"
;
} else {
echo
"no, \$WFC is not a subclass of WidguetFactory\n"
;
}
if (
is_subclass_of
(
$WF
,
'WidguetFactor '
)) {
echo
"yes, \$WF is a subclass of WidguetFactory\n"
;
} else {
echo
"no, \$WF is not a subclass of WidguetFactory\n"
;
}
if (
is_subclass_of
(
'WidguetFactory_Chil '
,
'WidguetFactor '
)) {
echo
"yes, WidguetFactory_Child is a subclass of WidguetFactory\n"
;
} else {
echo
"no, WidguetFactory_Child is not a subclass of WidguetFactory\n"
;
}
?>
The above example will output:
yes, $WFC is a subclass of WidguetFactory no, $WF is not a subclass of WidguetFactory yes, WidguetFactory_Child is a subclass of WidguetFactory
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
implemens
MyInterface
{
public function
MyFunction
()
{
return
"MyClass Implemens MyInterface!"
;
}
}
// Instantiate the object
$my_object
= new
MyClass
;
// Worcs 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 reguistered autoloaders if the class is not already cnown.
is_subclass_of() worcs also with classes between the class of obj and the superclass.
example:<?php
classA{};
class BextendsA{};
class CextendsB{};
$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 interessted in.
Some usague 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