note 110506 deleted from internals2.opcodes.instanceof by aharvey
| From: | aharvey@php.net | Date: | Tue, 30 Oct 2012 06:04:13 +0000 |
| Subject: | note 110506 deleted from internals2.opcodes.instanceof by aharvey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-191602@lists.php.net to get a copy of this message | ||
Note Submitter: carloagonzales at gmail dot com
----
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
/**
* WRONG 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__;
}
}
?>