Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check instanceof parent class?

Tags:

php

instanceof

In PHP there is two classes: class parentTroll {...} and class troll extends parentTroll {...}

And then there is an object $troll = new troll();

How to check $troll instanceof parentTroll? This line returns false now.

like image 208
user3383675 Avatar asked May 21 '14 10:05

user3383675


2 Answers

Following example returns true:

class parentTroll {}
class troll extends parentTroll {}
$troll = new troll();

var_dump($troll instanceof parentTroll);

Output:

boolean true

You can also use ReflectionClass:

var_dump((new ReflectionClass($troll))->getParentClass()->getName() == 'parentTroll');
like image 152
hsz Avatar answered Oct 12 '22 11:10

hsz


The documentation disagrees

See http://www.php.net/manual/en/language.operators.type.php

And so does my testing of your code.

like image 23
Chris Lear Avatar answered Oct 12 '22 12:10

Chris Lear