note 102988 added to language.operators.type
| From: | fbableus at osu1 dot php dot net | Date: | Fri, 18 Mar 2011 14:05:34 +0000 |
| Subject: | note 102988 added to language.operators.type | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-177979@lists.php.net to get a copy of this message | ||
If you want to test if a classname is an instance of a class, the instanceof operator won't
work.
<?php
$classname = 'MyClass';
if( $classname instanceof MyParentClass) echo 'Child of it';
else echo 'Not child of it';
?>
Will always output
Not child of it
You must use a ReflectionClass :
<?php
$classname = 'MyClass';
$myReflection = new ReflectionClass($classname);
if( $myReflection->isSubclassOf('MyParentClass')) echo 'Child of it';
else echo 'Not child of it';
?>
Will output the good result.
If you're testing an interface, use implementsInterface() instead of isSublassOf().
----
Server IP: 195.221.21.36
Probable Submitter: 82.127.98.176
----
Manual Page -- http://www.php.net/manual/en/language.operators.type.php
Edit -- https://master.php.net/note/edit/102988
Del: integrated -- https://master.php.net/note/delete/102988/integrated
Del: useless -- https://master.php.net/note/delete/102988/useless
Del: bad code -- https://master.php.net/note/delete/102988/bad+code
Del: spam -- https://master.php.net/note/delete/102988/spam
Del: non-english -- https://master.php.net/note/delete/102988/non-english
Del: in docs -- https://master.php.net/note/delete/102988/in+docs
Del: other reasons-- https://master.php.net/note/delete/102988
Reject -- https://master.php.net/note/reject/102988
Search -- https://master.php.net/manage/user-notes.php