note 34963 deleted from language.oop.newref by sniper
| From: | sniper@php.net | Date: | Sun, 11 Jan 2004 14:44:28 +0000 |
| Subject: | note 34963 deleted from language.oop.newref by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-63315@lists.php.net to get a copy of this message | ||
Note Submitter: amir at abiri dash e dot com
----
There is a much simpler and shorter workaround if you absolutly need the references to the
constructor's $this (i.e the original object):
class a
{
function a()
{
$this->self_ref = &$this;
}
}
$a = new a;
$a = &$a->self_ref;
now $a points to the original object, and allows the following code to work:
class a
{
function a($name)
{
$this->self_ref = &$this;
$this->name = $name;
$this->b = new b($this);
}
}
class b
{
function b(&$a)
{
$this->a = &$a;
}
}
$a = new a('a');
$a->b->a->name = 'b';
echo $a->name.'<br>'; // This would output 'a'.
// Now refer $a to the real object, the same one that the b class object refers to:
$a = &$a->self_ref;
$a->b->a->name = 'b';
echo $a->name.'<br>'; // Now this would output 'b'.