note 110507 added to internals2.opcodes.instanceof

From: Date: Tue, 30 Oct 2012 05:29:59 +0000
Subject: note 110507 added to internals2.opcodes.instanceof
Groups: php.notes php.notes 
Request: Send a blank email to php-notes+get-191594@lists.php.net to get a copy of this message
When checking if the an object of an unknown type is an instance of the class within its implementation and using magic constant __CLASS__ make sure to put the value of __CLASS__ to a variable first, before using instanceof. <?php /** * RIGHT WAY to determine class instance in a class method */ class TestClass { //static or what ever you prefer static function isClassInstance($variableWithUnknownClass){ return $variableWithUnknownClass instanceof __CLASS__; } } //Returns TRUE $object = new TestClass(); var_dump(TestClass::isClassInstance($object)); //Returns FALSE $object = new stdClass(); var_dump(TestClass::isClassInstance($object)); /** * WRONG WAY to determine class instance in a class method * RESULTS to a FATAL ERROR */ class TestClass { //static or what ever you prefer static function isClassInstance($variableWithUnknownClass){ return $variableWithUnknownClass instanceof __CLASS__; } } ?> ---- Server IP: 69.147.83.199 Probable Submitter: 222.127.44.156 ---- Manual Page -- http://www.php.net/manual/en/internals2.opcodes.instanceof.php Edit -- https://master.php.net/note/edit/110507 Del: integrated -- https://master.php.net/note/delete/110507/integrated Del: useless -- https://master.php.net/note/delete/110507/useless Del: bad code -- https://master.php.net/note/delete/110507/bad+code Del: spam -- https://master.php.net/note/delete/110507/spam Del: non-english -- https://master.php.net/note/delete/110507/non-english Del: in docs -- https://master.php.net/note/delete/110507/in+docs Del: other reasons-- https://master.php.net/note/delete/110507 Reject -- https://master.php.net/note/reject/110507 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#191594) next »