#40968 [Com]: Multiple intances of a singleton class when stored in session
| From: | b dot fore at mail dot com | Date: | Sun, 01 Apr 2007 01:42:08 +0000 |
| Subject: | #40968 [Com]: Multiple intances of a singleton class when stored in session | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-110987@lists.php.net to get a copy of this message | ||
ID: 40968
Comment by: b dot fore at mail dot com
Reported By: oriol dot gual at gmail dot com
Status: Open
Bug Type: Session related
Operating System: Windows XP SP2
PHP Version: 5.2.1
New Comment:
That's a gross abuse of ext/session dude.
Previous Comments:
------------------------------------------------------------------------
[2007-03-31 16:54:52] oriol dot gual at gmail dot com
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 this bug report at http://bugs.php.net/?id=40968&edit=1