#39254 [Opn]: Refcount error with static variables and object references
| From: | benoit dot heinrich at swisscom dot com | Date: | Wed, 25 Oct 2006 14:42:32 +0000 |
| Subject: | #39254 [Opn]: Refcount error with static variables and object references | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-103974@lists.php.net to get a copy of this message | ||
ID: 39254
User updated by: benoit dot heinrich at swisscom dot com
Reported By: benoit dot heinrich at swisscom dot com
Status: Open
Bug Type: Class/Object related
Operating System: Linux
PHP Version: 4.4.4
New Comment:
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
Previous Comments:
------------------------------------------------------------------------
[2006-10-25 14:11:12] 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:01:12] 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 13:54:06] 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
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=39254&edit=1