#40968 [NEW]: Multiple intances of a singleton class when stored in session
| From: | oriol dot gual at gmail dot com | Date: | Sat, 31 Mar 2007 16:54:52 +0000 |
| Subject: | #40968 [NEW]: Multiple intances of a singleton class when stored in session | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-110980@lists.php.net to get a copy of this message | ||
From: oriol dot gual at gmail dot com
Operating system: Windows XP SP2
PHP version: 5.2.1
PHP Bug Type: Session related
Bug description: Multiple intances of a singleton class when stored in session
Description:
------------
When storing an object of a singleton class in the session, you can have
more than one instance of that class in other subsequent executions.
Reproduce code:
---------------
class Singleton {
private static $instance;
private function __construct() {}
final public static function getInstance()
{
if (!isset(Singleton::$instance))
{
Singleton::$instance = &new Singleton;
}
return Singleton::$instance;
}
}
session_start();
if(!isset($_SESSION['singleton'])) $_SESSION['singleton'] =
Singleton::getInstance();
$test = Singleton::getInstance();
$anotherTest = Singleton::getInstance();
var_dump(Singleton::getInstance() === $test);
var_dump(Singleton::getInstance() === $anotherTest);
var_dump($test === $anotherTest);
var_dump(Singleton::getInstance() === $_SESSION['singleton']);
var_dump($test === $_SESSION['singleton']);
var_dump($anotherTest === $_SESSION['singleton']);
session_write_close();
Expected result:
----------------
Session not started (first execution)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
Subsequent executions
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
Actual result:
--------------
Session not started (first execution it's OK)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
Subsequent executions (fails, multiple instances)
bool(true)
bool(true)
bool(true)
bool(false)
bool(false)
bool(false)
--
Edit bug report at http://bugs.php.net/?id=40968&edit=1
--
Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=40968&r=trysnapshot44
Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=40968&r=trysnapshot52
Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=40968&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=40968&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=40968&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=40968&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=40968&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=40968&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=40968&r=support
Expected behavior: http://bugs.php.net/fix.php?id=40968&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=40968&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=40968&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=40968&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=40968&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=40968&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=40968&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=40968&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=40968&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=40968&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=40968&r=mysqlcfg