Re: Reference oddities...

From: Date: Sat, 26 Aug 2000 21:15:40 +0000
Subject: Re: Reference oddities...
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-13811@lists.php.net to get a copy of this message
> I boiled a problem I'm having down to the code below: > > <? > class Main { > function Main() { > $this->test1 = new Test1(&$this, "first test"); > $this->test2 = new Test2(&$this, "second test"); > } > } Don't call functions with references, declare functions with references. class Main { function Main() { $this->test1 = new Test1($this, "first test"); $this->test2 = new Test2($this, "second test"); } } class Test1 { function Test1(&$super, $name) { $this->super = $super; $this->name = $name; } } > > class Test1 { > function Test1($super, $name) { > $this->super = $super; > $this->name = $name; > } > } > > class Test2 { > function Test2($super, $name) { > $this->super = $super; > $this->name = $name; > } > } > > $m = new Main(); > print "\$m->test1->super->test2->name: " . > $m->test1->super->test2->name > . "<BR>"; > print "\$m->test2->super->test1->name: " . > $m->test2->super->test1->name > . "<BR>"; > > ?> > > When I run this script (PHP4.0.0,Apache1.3.12 on Mandrake 7.0), I get > this output: > > $m->test1->super->test2->name: > $m->test2->super->test1->name: first test > You might want to go as far as: $t1 = $m->test1; $t1s = $t1->super; $t1s2 = $t1s->test2; $t1s2n = $t1s2->name; But your code should work in PHP4. > The classes Test1 and Test2 both have a constructor that is called with > a reference > to the super-class Main. The output I was expecting is: > > $m->test1->super->test2->name: second test > $m->test2->super->test1->name: first test > > Of course, if I change the code to: > <? > > class Main { > function Main() { > $this->test2 = new Test2(&$this, "second test"); > $this->test1 = new Test1(&$this, "first test"); > } > } > > class Test1 { > function Test1($super, $name) { > $this->super = $super; > $this->name = $name; > } > } > > class Test2 { > function Test2($super, $name) { > $this->super = $super; > $this->name = $name; > } > } > > $m = new Main(); > print "\$m->test1->super->test2->name: " . > $m->test1->super->test2->name > . "<BR>"; > print "\$m->test2->super->test1->name: " . > $m->test2->super->test1->name > . "<BR>"; > > ?> > > I get: > > $m->test1->super->test2->name: second test > $m->test2->super->test1->name: > > What am I doing wrong? A reference is just like a pointer, right? > Shouldn't the classes Test1 > and Test2 be able to reference all members of their super-class Main, > even if they were instantiated > later than themselves??? > > Thanks for any explanation! > > Arjan > > -- > > ººººººººººººººººººººººººººººººººººººººººººººººººº > Arjan Haverkamp aka: void > Borneolaan 21 url: http://www.avoid.org > 1217 GX Hilversum email: void@avoid.org > The Netherlands "There's no gravity, > +31-35-6234660 the earth just sucks..." > > ººººººººººººººººººººººººººººººººººººººººººººººººº > > >

« previous php.general (#13811) next »