Coming back to this questions, this is how I figured to deal with the question raised on this post, sending parent class reference to child class: new _child($name, $this):
class _parent {
function _parent($name) {
$this->name = "I'm $name";
$this->childs = array();
}
function addToName($name) {
$this->name .= " + father of " . $name;
}
function addChild($name) {
$this->childs[] = new _child($name, $this);
}
}
class _child {
function _child($name, $parent) {
$this->name = "I'm $name";
$this->brothers = 0;
$parent->addToName($name);
foreach ($parent->childs as $child) {
$child->hasBrother($name);
}
}
function hasBrother($name) {
$this->name .= " + older brother of $name";
$this->brothers = 1;
}
}
$a = new _parent("A");
$a->addChild("B1");
$a->addChild("B2");
$a->addChild("B3");
echo "<pre>"; print_r($a); echo "</pre>";
Any comments are welcome!