Re: Refering to an object from within an array variable in another class
| From: | Bas Jobsen | Date: | Tue, 03 Sep 2002 17:59:01 +0000 |
| Subject: | Re: Refering to an object from within an array variable in another class | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-115074@lists.php.net to get a copy of this message | ||
> Any insight appreciated. In a best case if someone can add in the required
> '&'s to the following code that would be great.
<?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)
{
$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->SetBarMarklarSyntaxB('one','eins') ;
$foo->SetBarMarklarSyntaxB('two','zwei') ;
print_r($foo) ;
echo '</pre>' ;
?>