php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40968 Multiple intances of a singleton class when stored in session
Submitted: 2007-03-31 16:54 UTC Modified: 2007-04-03 18:46 UTC
From: oriol dot gual at gmail dot com Assigned:
Status: Not a bug Package: Session related
PHP Version: 5.2.1 OS: Windows XP SP2
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: oriol dot gual at gmail dot com
New email:
PHP Version: OS:

 

 [2007-03-31 16:54 UTC] 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)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-04-01 01:42 UTC] b dot fore at mail dot com
That's a gross abuse of ext/session dude.
 [2007-04-02 13:24 UTC] smlerman at gmail dot com
The only thing that can guarantee only one instance of your class existing is that Singleton::$instance is the only place you store an instance and getInstance() is the only way to retrieve an instance. Once you create a second place that an instance can be stored, such as storing an instance somewhere else (in the session data) and destroying the instance in the static property (which happens when the script ends), you create a second way to retrieve an entirely different instance. In general, there's no 100% guaranteed way to ensure that only one instance of a given class can ever exist.
 [2007-04-03 18:46 UTC] tony2001@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 16 16:01:34 2024 UTC