my actual code is complex so here is a simple but relevant illustration:
class base {
var $child1;
var $child2;
function xcv() {
$this->child1 = new objChild1();
$this->child2 = new objChild2();
}
}
class objChild1 {
var $fruit = "apple开发者_开发技巧";
}
class objChild2 {
function getChild1Fruit() {
echo parent::child1->fruit;
}
}
fairly straight forward but what if objGrandchild1 wants to call child2 etc... is it like parent::parent::child1->fruit?
any tips in this area would be appreciated
===== EDIT ===== Sorry I just realised that parent belongs to the use of extend so probably nothing to do with it
You got it right in the edit you made. An object has no realisation of the object that's being used in. You could get this reference through a parameter, though.
class base {
var $child1;
var $child2;
function xcv() {
$this->child1 = new objChild1();
$this->child2 = new objChild2($this);
}
}
class objChild1 {
var $fruit = "apple";
}
class objChild2 {
objChild2($parent) {
$this->parent = $parent;
}
function getChild1Fruit() {
echo $this->parent->child1->fruit;
}
}
精彩评论