I wonder if it is possible to check the inheritance of a class in a static way.
In cases I get just a class name with its namespace I can do:
$oObject = new $sClassName();
if(is_a($oObject, $sParentClassName)) { return true; }
But here it is mandatory to have an instance of that class.
The only static check I have found so far is the following:
if(method_exists($sClassName, $sMethodNameFromParent)) { return true; }
But checking just for a method is no good, because I can't be sure whether the class in question inherited it from that specific parent, some other parent or implemented it all by itself.
Is there any way to check the inheritance in a static manner? Thanks in advance!
You can use Reflection::isSubclassOf for this.
class A {}
class D {}
class B extends D {}
class C extends B {}
$reflected = new ReflectionClass('C');
echo $reflected->isSubclassOf('B'); // true
echo $reflected->isSubclassOf('D'); // true
echo $reflected->isSubclassOf('A'); // false
No instance of C
will be created, and will work even if the class has __construct
arguments
$oObject = new $sClassName();
if( get_parent_class($oObject) == $sParentClassName ) { return true; }