lemar hadad lemar hadad - 6 months ago 16
PHP Question

How PHP make compsition?

in c++ :

class first{
int x;
first ();
}

class tow{
int y;
first fst;
tow ();
}

class three{
int z;
tow tw;
three ();
}

void main(){
three obj = new three;
int var=obj . tw . fst . x; //how I can access to x from a obj ??
}


my question is : what the syntax of PHP to access to data member like (x) from an instance from class like (three)???

Answer

is this what you are looking for:

<?

class first{
    public $x;

    public function __construct(){
        $this->x = 42;
    }

}

class tow{
    public $y;
    public $fst;

    public function __construct(){
        $this->fst = new first;
    }

}

class three{
    public $z;
    public $tw;

    public function __construct(){
        $this->tw = new tow;
    }
}

$obj = new three;
// assuming there is a constructors that fill eveyhing correct.
$var = $obj->tw->fst->x;

echo $var . "\n";

Here is working example.