note 56405 deleted from language.operators.type by colder

From: Date: Sat, 24 Feb 2007 10:02:27 +0000
Subject: note 56405 deleted from language.operators.type by colder
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-123809@lists.php.net to get a copy of this message
Note Submitter: MooGoo ---- 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"; ?>

« previous php.notes (#123809) next »