Arrays of objects of arrays of objects...

From: Date: Sun, 23 Jul 2000 06:25:43 +0000
Subject: Arrays of objects of arrays of objects...
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-7755@lists.php.net to get a copy of this message
Hi ho, I'm new here and hope this isn't an old tired subject. I couldn't find anything in the archives. I have a class that contains an array of the same class. I can't get my code to work when I refer to an object in the array of another objects. Consider this code : <?php class SomeObject { var $id; var $subObjects; function SomeObject( $newID ) { $this->id = $newID; $this->subObjects[0] = 0; // otherwise $this->subObjects is treated as a string. } function AddSubObject( $newID ) { $this->subObjects[] = new SomeObject( $newID ); } function DoObject( ) { echo("Starting to do object <b>$this->id</b><br>\n"); $oCnt = count( $this->subObjects ); for ($t=1; $t<$oCnt; $t++) { // start at [1] because [0] has a 0 place holder. $this->subObjects[$t]->DoObject(); } echo("Finishing object <b>$this->id</b><br>\n"); } } $testObj = new SomeObject("MAIN"); $testObj->AddSubObject("SUB 01 00"); $testObj->subObjects[1]->AddSubObject("SUB 01 XX"); $testObj->DoObject(); ?> The code in the method DoObject( ) -- $this->subObjects[$t]->DoObject(); -- gives me a parse error. If I replace this line with the two lines : $tObj = $this->subObjects[$t]; $tObj->DoObject(); Things work as they should. Here I don't have a problem with the little work around. However, this work around make things weird later on. The line $testObj->subObjects[1]->AddSubObject("SUB 01 XX"); also gives me a parse error. In this case, I don't like the solution : $tObj->$this->subObjects[1]; $tObj->AddSubObject("SUB 01 XX"); $this->subObjects[1] = $tObj; nor $tObj = new SomeObject("SUB 01 XX"); $this->subObjects[1] = $tObj; Is it possible to do 'cascading' references with objects in PHP? $testObj->subObjects[1]->AddSubObject("SUB 01 XX"); is a reference to a method of an object referenced by the property of another object. Hence two "->" in the same line. Legal?? As a side question -- is there a way to get var $subObjects; to default to type array? If I don't do the $this->subObjects[0] = 0; , subObjects defaults to type string and the function count( $this->subObjects ); returns 1 instead of 0 in the case where there are no sub objects. I hope my question is clear enough to solicit a response. Thanks in advance. Take care and all the best, Gary Gary Kiser -- Tuba gary@kiser.com ICQ: 21035940 Clermont-Ferrand, France

« previous php.general (#7755) next »