#39254 [NEW]: Refcount error with static variables and object references
| From: | benoit dot heinrich at swisscom dot com | Date: | Wed, 25 Oct 2006 13:54:06 +0000 |
| Subject: | #39254 [NEW]: Refcount error with static variables and object references | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-103968@lists.php.net to get a copy of this message | ||
From: benoit dot heinrich at swisscom dot com
Operating system: Linux
PHP version: 4.4.4
PHP Bug Type: Class/Object related
Bug description: Refcount error with static variables and object references
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 bug report at http://bugs.php.net/?id=39254&edit=1
--
Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=39254&r=trysnapshot44
Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=39254&r=trysnapshot52
Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=39254&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=39254&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=39254&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=39254&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=39254&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=39254&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=39254&r=support
Expected behavior: http://bugs.php.net/fix.php?id=39254&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=39254&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=39254&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=39254&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=39254&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=39254&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=39254&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=39254&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=39254&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=39254&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=39254&r=mysqlcfg