#27219 [Ver->Bgs]: Unable to create static instance if no member vars are declared

From: Date: Wed, 22 Jun 2005 19:30:03 +0000
Subject: #27219 [Ver->Bgs]: Unable to create static instance if no member vars are declared
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-80852@lists.php.net to get a copy of this message
ID: 27219 Updated by: tony2001@php.net Reported By: ivo at ibuildings dot nl -Status: Verified +Status: Bogus Bug Type: Scripting Engine problem Operating System: * PHP Version: 4CVS-2005-01-25 (4 only!) New Comment: Because in PHP4 empty object is == NULL, so you have to use === operator, as you're doing it with empty strings, 0 etc. No PHP bug here, it's well documented. See here: http://www.php.net/manual/en/migration5.incompatible.php Previous Comments: ------------------------------------------------------------------------ [2004-02-11 12:31:38] sniper@php.net Works fine with latest PHP5 CVS checkout. Only borks in PHP 4. ------------------------------------------------------------------------ [2004-02-11 08:49:20] ivo at ibuildings dot nl Description: ------------ When a class does not have member variables, creating a static instance of the class becomes impossible. See the code below. If the member variable is declared, one onstance is created, and each next call to getInstance will return the existing instance.Without the member var, new instances are created every time. Reproduce code: --------------- class mySingleton { // var $m_dummy = ""; // uncommenting this line is a // workaround function &getInstance() { static $s_instance = NULL; if ($s_instance == NULL) { echo "Creating a new instance<br>"; $s_instance = new mySingleton(); } else { echo "Using existing instance<br>"; } return $s_instance; } } $tmp = &mySingleton::getIntance(); $tmp2 = &mySingleton::getIntance(); Expected result: ---------------- Creating a new instance Using existing instance Actual result: -------------- Creating a new instance Creating a new instance ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=27219&edit=1

« previous php.bugs (#80852) next »