note 15996 deleted from language.variables.scope by jimw
| From: | jimw@php.net | Date: | Sat, 08 Dec 2001 21:42:43 +0000 |
| Subject: | note 15996 deleted from language.variables.scope by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-22217@lists.php.net to get a copy of this message | ||
In a large project recently undertaken, we have been using the PEAR virtual destructors.
We have been using a small code snippet to allow pseudo-singletons within the OO code:
class MyClass
{
function &getInstance()
{
static $instance = null;
if ($instance === null) {
$instance = new MyClass();
}
return $instance;
}
}
$myobj =& MyClass::getInstance();
However, this break PEAR destructors as $instance is created with new, rather than &new. If
&new is used to create the static variable, it is destroyed on function exit, and therefore is
null again the second time around.