|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2006-10-25 14:01 UTC] tony2001@php.net
[2006-10-25 14:11 UTC] benoit dot heinrich at swisscom dot com
[2006-10-25 14:42 UTC] benoit dot heinrich at swisscom dot com
[2006-10-25 14:49 UTC] tony2001@php.net
[2006-10-25 22:32 UTC] judas dot iscariote at gmail dot com
[2007-06-19 23:51 UTC] helping at hotmail dot com
[2008-07-11 21:23 UTC] jani@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 05:00:01 2025 UTC |
Description: ------------ Hello all, I've a script that keep an object into a cache to speed up performances. To access the cached instance, I'm using a static method of the class MyFactory::getInstance(); In that method I need to have a reference on the instance itself because the attached params also need to have a reference on their owner. Case 1 --------------------------------- If I do $instance =& new MyFactory(); in the getInstance() method then I have this: $a->code: 2 $a->params[toto]->parent->code: 2 $b->code: 1657232709 $b->params[toto]->parent->code: 1657232709 So the thing here is that the static keyword does not seems to keep a refcount on the instance when it's created the first time. So each time you call the getInstance() it creates a new instance instead of keeping the first created one. Case 2 --------------------------------- If I do $instance = new MyFactory(); in the getInstance() method then I have this: $a->code: 2 $a->params[toto]->parent->code: 576672258 $b->code: 2 $b->params[toto]->parent->code: 576672258 Here we have the result we can expect accordingly to the PHP4 documentation, but this not what I need. Please, can you investigate on that one ? Cheers, /Benoit Reproduce code: --------------- <?php error_reporting(E_ALL); /** * This is an example of a param implementation class. * The constructor always need a reference on its parent * (here it's not really needed, but in the real code it is) */ class toto { var $parent; function toto(&$parent) { $this->parent =& $parent; } } /** * This the factory class used to create instances of params. * The factory instance must be unique. */ class MyFactory { var $code; var $params = array(); function MyFactory() { $this->code = rand(); $this->initParam('toto'); } /** * Initialize a parameter */ function initParam($param) { $this->params[$param] =& new $param($this); } /** * Get the unique instance of the factory */ function & getInstance() { static $instance; // Due to the bug $instance is always null if (is_null($instance)) $instance =& new MyFactory(); return $instance; } } $a =& MyFactory::getInstance(); $a->code = 2; print '$a->code: ' . $a->code . "\n"; print '$a->params[toto]->parent->code: ' . $a->params['toto']->parent->code . "\n"; $b =& MyFactory::getInstance(); print '$b->code: ' . $b->code . "\n"; print '$b->params[toto]->parent->code: ' . $b->params['toto']->parent->code . "\n"; ?> Expected result: ---------------- $a->code: 2 $a->params[toto]->parent->code: 2 $b->code: 2 $b->params[toto]->parent->code: 2 Actual result: -------------- $a->code: 2 $a->params[toto]->parent->code: 2 $b->code: 1657232709 $b->params[toto]->parent->code: 1657232709