RE: [PHP] OBJECT£ºWHAT'S THE RALATI ON BETWEEN A,B AND C?
| From: | Tim Ward | Date: | Mon, 18 Mar 2002 11:11:12 +0000 |
| Subject: | RE: [PHP] OBJECT£ºWHAT'S THE RALATI ON BETWEEN A,B AND C? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-88909@lists.php.net to get a copy of this message | ||
The line you've marked prints the "value" attribute of $a->b->a, which is a
pointer to the base object ($a). You have just changed the "value" attribute
of this to 11 and it prints 11 ... what's wrong?
Tim Ward
Internet Chess www.chessish.com <http://www.chessish.com>
----------
From: Oliver Heinisch [SMTP:creaction@compuserve.com]
Sent: 17 March 2002 12:20
To: php-general@lists.php.net
Subject: Re: [PHP] OBJECT£ºWHAT'S THE RALATION BETWEEN A,B AND C?
At 17.03.2002 10:01, you wrote:
>
><?php
>class A
>{
> function A($i)
> {
> $this->value = $i;
> // try to figure out why we do not need a reference here
> $this->b = new B($this);
> }
>
> function createRef()
> {
> $this->c = new B($this);
> }
>
> function echoValue()
> {
> echo "<br>","class ",get_class($this),':
> ',$this->value;
> }
>}
>
>
>class B
>{
> function B(&$a)
> {
> $this->a = &$a;
> }
>
> function echoValue()
> {
> echo "<br>","class ",get_class($this),':
',$this->a->value;
> }
>}
>
>// try to undestand why using a simple copy here would yield
>// in an undesired result in the *-marked line
>$a =new A(10);
>$a->createRef();
>
>$a->echoValue();
>$a->b->echoValue();
>$a->c->echoValue();
>
>$a->value = 11;
>
>$a->echoValue();
>$a->b->echoValue(); // *
>$a->c->echoValue();
>
>?>
Even if I don´t understand what your code does, shouldn´t you
give any variable a place to be stored so it schould look like
class foo
{
var $a;
var $b;
function fooplus()
{
$this -> a = foofoo;
and so on ??
}
}
HTH Oliver