Re: OOP Problems

From: Date: Sat, 14 Jul 2001 18:04:32 +0000
Subject: Re: OOP Problems
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-57895@lists.php.net to get a copy of this message
Hi Andrew! On Sat, 14 Jul 2001, Andrew Kirilenko wrote: > Hello! > > I have following problem: > > <? > $container = new ccontainer(); > $container->init(); > $container->test1->foo(); > $container->test2->foo(); > $container->test2->owner->test1->foo(); [*] > $container->test1->owner->test2->foo(); > > class ccontainer > { > function ccontainer() > { > } > > function init() > { > $this->test1 = new ctest(&$this); > $this->test2 = new ctest(&$this); > } > } > > class ctest > { > function ctest(&$owner) > { > $this->owner = $owner; > } > > function foo() > { > echo "test!<br>"; > } > } > ?> > > Output of this script is: > ---> > test! > test! > test! > Fatal error: Call to a member function on a non-object in c:\www\a.php on > line 8 > <--- > > How to solve this problem??? > at the moment marked [*] you are using test1->owner test1->owner is ccontainer *before* init() had the chance to set test2 to it, so the class has no such property "test2". try a print_r($container) right after $cointainer->init() to see what I mean. actually, you will notice that test2->owner doesn't have test2 member too! cause at the moment it was created, the ccontainer was incomplete (the expression $this->test2 = new ctest (&$this) evaluates right-to-left) a quick hack would be to have a circular reference, by saying: $this->owner =& $owner; // note the &amp; :) but the way you create the classes seems just trouble. what exactly are you trying to achive? -- teodor

« previous php.general (#57895) next »