|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2004-02-11 12:31 UTC] sniper@php.net
[2005-06-22 21:30 UTC] tony2001@php.net
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 23:00:01 2025 UTC |
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