OOP: retrieve 'fathers' attributes from the 'child' object?

You can't unless you tell the sons who their father is. This could be done by adding a setFather() method to the son and calling in the addSon() method of the father eg class Son { protected $_father; // ... public function setFather($father) { $this->_father = $father; } public function getFather() { return $this->_father; } } class Father { // ... public function AddSon($son_id, $son_name, $son_age){ $sonHandler = new Son($son_id, $son_name, $son_age); $sonHandler->setFather($this); $this->sons = $sonHandler; return $sonHandler; } } As a side note, I wouldn't create the son within the AddSon method, I would have that method take an already created son as its parameter.

You can't unless you tell the sons who their father is. This could be done by adding a setFather() method to the son and calling in the addSon() method of the father. Eg.

Class Son { protected $_father; // ... public function setFather($father) { $this->_father = $father; } public function getFather() { return $this->_father; } } class Father { // ... public function AddSon($son_id, $son_name, $son_age){ $sonHandler = new Son($son_id, $son_name, $son_age); $sonHandler->setFather($this); $this->sons = $sonHandler; return $sonHandler; } } As a side note, I wouldn't create the son within the AddSon method, I would have that method take an already created son as its parameter.

YOu could even use a PHP magic method so that $son->job() automatically went and got the job from the father; but you'd still have had to tell the son who the father was. – Colin Fine Jul 28 '09 at 14:10 Brenton solution works good, but please, exmplain even the 'magic method' – DaNieL Jul 28 '09 at 14:13 2 It wouldn't have to be magic, it'd just be a method in the son that looked like: public function getFatherJob() { return $this->getFather()->job; } I'd personally rather chain the methods in most cases, but up to you. – Brenton Alker Jul 28 '09 at 14:20.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions