|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits              [2007-04-01 01:42 UTC] b dot fore at mail dot com
  [2007-04-02 13:24 UTC] smlerman at gmail dot com
  [2007-04-03 18:46 UTC] tony2001@php.net
 | |||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 06:00:01 2025 UTC | 
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)