View Single Post
Old 11-13-2008, 02:04 AM   #1 (permalink)
tony
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default How to access the method of an object that is inside an object

Hi guys and gals, once again i hit bump in this project.
the thing is that I have 2 classes, well I think it's better if i put the code first:

PHP Code:
<?php
class foo{
    private 
$a;
    private 
$b;
    
    function 
__construct(){
        
$this->a=3;
        
$this->b=3;
    }
    
    public function 
getA(){    return $this->a; }
    public function 
getB(){    return $this->b; }
    public function 
setA($t){ $this->a=$t; }
    public function 
setB($t){ $this->a=$t; }
}

clas bar{
    
$private c;
    
$private d;
    
    function 
__construct(){
        
$this->c=10;
        
$this->d= new foo;
    }
    
    public function 
getC(){ return $this->}
    public function 
getD(){ return $this->}
    public function 
setC($t){ $this->c=$t; }
    public function 
setD($t){ $this->d=$t; }
}
?>
now the problem rises when i want to get the getA() method from an instance of bar, like this:

PHP Code:
<?php
$house
=new Bar;
echo 
$house->getD()->getA(); // it should output 3 ideally 
?>
but I get this "Fatal error: Can't use method return value in write context"

any ideas on what i am doing wrong? thanks for the help in advance.
tony is offline  
Reply With Quote