|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #39254 Refcount error with static variables and object references (PHP4 only)
Submitted: 2006-10-25 13:54 UTC Modified: 2008-07-11 21:23 UTC
From: benoit dot heinrich at swisscom dot com Assigned:
Status: Wont fix Package: Class/Object related
PHP Version: 4.4.4 OS: Linux
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [2006-10-25 13:54 UTC] benoit dot heinrich at swisscom dot com
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 ?


Reproduce code:

 * 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();
	 * 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


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2006-10-25 14:01 UTC]
Please try using this CVS snapshot:
For Windows:

 [2006-10-25 14:11 UTC] benoit dot heinrich at swisscom dot com
I'm sorry but I'm using php 4.4 and I can't use PHP 5 for a lot of reasons.
Do you have any fix for php 4.4 ?

 [2006-10-25 14:42 UTC] benoit dot heinrich at swisscom dot com
I tried to find a workaround, and when I tried to use the 'global' keyword instead of the 'static' then it gives exactly the same problem.

I'm still searching for a workaround but for now I have nothing.

 [2006-10-25 14:49 UTC]
I really doubt there will be any active development in 4.x branch, for we're mostly working on 5.x and 6.x at the moment.
 [2006-10-25 22:32 UTC] judas dot iscariote at gmail dot com
benoit: is very likely you are on your own now, PHP5 is out since years and certainly solve your problem, PHP4 active development has ceased permanently, it is only open to security fixes or severe regresions that affects large part of the user base.
 [2007-06-19 23:51 UTC] helping at hotmail dot com
not sure if you still need it, there is a workaround here:
 [2008-07-11 21:23 UTC]
We are sorry, but we can not support PHP 4 related problems anymore.
Momentum is gathering for PHP 6, and we think supporting PHP 4 will
lead to a waste of resources which we want to put into getting PHP 6

PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Tue Aug 29 15:01:52 2017 UTC