php.net |  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: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: benoit dot heinrich at swisscom dot com
New email:
PHP Version: OS:

 

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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-10-25 14:01 UTC] tony2001@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5.2-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5.2-win32-latest.zip


 [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 ?

Cheers,
/Benoit
 [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.


Cheers,
/Benoit
 [2006-10-25 14:49 UTC] tony2001@php.net
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:
http://www.phphacks.com/content/view/40/33/
 [2008-07-11 21:23 UTC] jani@php.net
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
ready.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 07:01:29 2024 UTC