views:

97

answers:

1

Hi guys, here i am again ;)

My problem atm is with nested php classes, i have, for example, a class like this one:

class Father{
    public $father_id;
    public $name;
    public $job;
    public $sons;
    public function __construct($id, $name, $job){
        $this->father_id = $id;
        $this->name = $name;
        $this->job = $job;
        $this->sons = array();
    }

    public function AddSon($son_id, $son_name, $son_age){
        $sonHandler = new Son($son_id, $son_name, $son_age);
        $this->sons[] = $sonHandler;
        return $sonHandler;
    }

    public function ChangeJob($newJob){
        $this->job = $newJob;
    }
}

class Son{
    public $son_id;
    public $son_name;
    public $son_age;
    public function __construct($son_id, $son_name, $son_age){
        $this->son_id = $son_id;
        $this->son_name = $son_name;
        $this->son_age = $son_age;
    }
    public function GetFatherJob(){
        //how can i retrieve the $daddy->job value??
    }
}

that's it, a useless class to explain my problem. What im trying to do is:

$daddy = new Father('1', 'Foo', 'Bar');
//then, add as many sons as i need:
$first_son = $daddy->AddSon('2', 'John', '13');
$second_son = $daddy->AddSon('3', 'Rambo', '18');
//and i can get here with no trouble. but now, lets say i need
//to retrieve the daddy's job referencing any of his sons... how?
echo $first_son->GetFatherJob(); //?

So, every son must be indipendent from each other but inherits the attributes and values from the father one..

I've tryed with inheritance:

class Son extends Father{
[....]

But i'll have to declare the father attributes every time i add a new son..otherwise father's attributes will be null

Any help?

+3  A: 

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.

Brenton Alker
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
Brenton solution works good, but please, exmplain even the 'magic method'
DaNieL
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