Reputation: 1222
What I'm trying to do is to check some class inheritance before instantiating
class A{}
class B extends A{}
class C{}
i want to check B, C classes for their inheritance before moving forward, if they inherit A then move on, otherwise i will not instantiate.
// That's not what i want
$B = new B();
var_dump($B instance of A); // Valid => true
// That's what i want
var_dump(B instance of A); // Not valid
But i'm just wondering if that's possible here.
Thanks.
Upvotes: 2
Views: 263
Reputation: 5796
Yes, it's possible with:
is_subclass_of();
Example below from http://php.net/manual/en/function.is-subclass-of.php:
// usable only since PHP 5.0.3
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";
}
Upvotes: 6