note 56405 added to language.operators.type

From: Date: Thu, 01 Sep 2005 14:42:57 +0000
Subject: note 56405 added to language.operators.type
Groups: php.notes 
Request: Send a blank email to php-notes+get-94611@lists.php.net to get a copy of this message
instanceof can compare a class instance to a string which contains a class name. However that string must be contained in a variable, it will not work as a return value from a function (for instance, get_class()) or even from a regular inline string. <?php class Moo { function Goo() { $moo = "I do nothing"; } } $moo = new Moo; //Will fail if ($moo instanceof "Moo") echo "Goo\n"; //Will also fail if ($moo instanceof get_class($moo)) echo "Goo\n"; //However... //Will succeed $className = get_class($moo); if ($moo instanceof $className) echo "Goo\n"; //Will also succeed $className = "Moo"; if ($moo instanceof $className) echo "Goo\n"; ?> ---- Manual Page -- http://www.php.net/manual/en/language.operators.type.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+56405 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56405&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56405&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56405&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56405&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56405&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56405&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+56405&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#94611) next »