note 20558 added to language.references
| From: | info at paralight dot ru | Date: | Mon, 08 Apr 2002 07:34:36 +0000 |
| Subject: | note 20558 added to language.references | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-28946@lists.php.net to get a copy of this message | ||
A have found interesting strange in references to classes.
class Child{
function Child(&$parent)
{
$this->parent=&$parent;
$this->parent->var=10;
}
function doit()
{
$this->parent->var=20;
}
}
class A{
function A()
{
$this->child=new Child($this);
}
function doit()
{
$this->child->doit();
echo $this->var;
}
}
$a = new A;
$a->doit();
-----------------
In result we have 10, not 20.
And, if we call $a->doit(); from constructor À():
function A()
{
$this->child=new Child($this);
$this->doit();
}
we resulting 20.
The way to takearound this is make empty constructor, move all from it to function constr(), and run
some like:
$a=new A;
$a->me=&$a;
$a->constr();
$a->doit();
in constr() we have $this->child=new Child($this->me); [not Child($this)].
and, as result we have 20 as would be expected.
That's this? Am I missunderstand something or it is PHP bug?
--
http://www.php.net/manual/en/language.references.php
http://master.php.net/manage/user-notes.php?action=edit+20558
http://master.php.net/manage/user-notes.php?action=delete+20558
http://master.php.net/manage/user-notes.php?action=reject+20558