php agreggating objects , need explanation -
i have example provided book :
class address { protected $city; public function setcity($city) { $this -> city = $city; } public function getcity() { return $this -> city; } } class person { protected $name; protected $address; public function __construct() { $this -> address = new address; } public function setname($name) { $this -> name = $name; } public function getname() { return $this -> name; } public function __call($method, $arguments) { if (method_exists($this -> address, $method)) { return call_user_func_array( array($this -> address, $method), $arguments); } } } $rasmus=new person; $rasmus->setname('rasmus lerdorf'); $rasmus->setcity('sunnyvale'); print $rasmus->getname().' lives in '.$rasmus->getcity().'.';
however have problem understanding code.
how use __construct agreggate object , why need __call function?
__construct
constructor person
class , instantiates each person
object.
__call
allows setcity
method called on object of type person
. class person
not possess method named setcity
, using __call
, passes method call $address
instance variable of type address
. address
class contains actual definition of setcity
.
both php magic functions: http://php.net/manual/en/language.oop5.magic.php
Comments
Post a Comment