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?