note 74455 added to language.oop5.patterns
| From: | edunreal at gmail dot com | Date: | Thu, 12 Apr 2007 10:10:01 +0000 |
| Subject: | note 74455 added to language.oop5.patterns | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-125321@lists.php.net to get a copy of this message | ||
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
----
Server IP: 69.147.83.197
Probable Submitter: 81.240.59.110
----
Manual Page -- http://www.php.net/manual/en/language.oop5.patterns.php
Edit -- https://master.php.net/note/edit/74455
Del: integrated -- https://master.php.net/note/delete/74455/integrated
Del: useless -- https://master.php.net/note/delete/74455/useless
Del: bad code -- https://master.php.net/note/delete/74455/bad+code
Del: spam -- https://master.php.net/note/delete/74455/spam
Del: non-english -- https://master.php.net/note/delete/74455/non-english
Del: in docs -- https://master.php.net/note/delete/74455/in+docs
Del: other reasons-- https://master.php.net/note/delete/74455
Reject -- https://master.php.net/note/reject/74455
Search -- https://master.php.net/manage/user-notes.php