note 74455 deleted from language.oop5.patterns by colder
| From: | colder@php.net | Date: | Thu, 12 Apr 2007 11:27:05 +0000 |
| Subject: | note 74455 deleted from language.oop5.patterns by colder | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-125326@lists.php.net to get a copy of this message | ||
Note Submitter: edunreal at gmail dot com
Reason: simply wrong
----
I found that using intanceof to check whether the static variable is set, doesn't work.
Consider the following example:
<?php
class Example{
private static $_instance = NULL
public static function getInstance(){
if(!self::$_instance instanceof self){
echo "creating new Object";
self::$_instance = new self;
}
return self::$_instance;
}
}
// Let's initiate an Example object:
$obj = Example::getInstance();
// outputs: "creating new Object"
// let's try to grab an instance of the Example object
$obj = Example::getInstance();
// here we expect nothing to show up, instead, "creating new Object" appears
?>
The solution is very simple, use isset instead of instanceof. Probably everyone knew this allready,
but i didn't.
cheers