Refering to an object from within an array variable in another class

From: Date: Tue, 03 Sep 2002 13:44:36 +0000
Subject: Refering to an object from within an array variable in another class
Groups: php.general 
Request: Send a blank email to php-general+get-115031@lists.php.net to get a copy of this message
Not sure I'm using the right terminology here, but the following code example explains my problem better than any words can. I have a class - Foo - that includes an array variable, each member of which is an instance of the class Bar. I want the interface to the variables within each instance of Bar to be via a method of class Foo, but I can't seem to hammer out the correct syntax; I figure that - at some point - I ought to be passing each instance of Bar by reference rather than value, but can't work out where - and how - I should be doing so. Any insight appreciated. In a best case if someone can add in the required '&'s to the following code that would be great. TIA <?php echo '<pre>' ; error_reporting ( E_ALL ) ; class Foo { var $bars = array() ; function AddBar($id,$text) { $obj = new Bar ( $text ) ; $this->SetBar ( $id , $obj ) ; } function SetBar($id,$bar) { $this->bars[$id] = $bar ; } function GetBar($id) { return $this->bars[$id] ; } function SetBarMarklarSyntaxA ($id,$value) { /* This syntax works... */ $this->bars[$id]->SetMarklar($value) ; } function SetBarMarklarSyntaxB ($id,$value) { /* But this syntax doesn't: and this would be prefered, as it doesn't directly access the 'bars' variable... */ $reference_to_bar = $this->GetBar($id) ; $reference_to_bar->SetMarklar($value) ; } } class Bar { var $marklar = '' ; function Bar($marklar) { $this->SetMarklar($marklar) ; } function SetMarklar($value) { $this->marklar = $value ; } } //----------- $foo = new Foo ; $foo->AddBar('one','un') ; $foo->AddBar('two','duex') ; print_r($foo) ; //Works $foo->SetBarMarklarSyntaxA('one','eins') ; // Doesn't $foo->SetBarMarklarSyntaxB('two','zwei') ; print_r($foo) ; echo '</pre>' ; ?> -- Andy Warwick Creed New Media Design http://www.creed.co.uk/src/unet/index.htm

« previous php.general (#115031) next »