OOP - $parent?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

OOP - $parent?

Post by MarK (CZ) »

Is there any way to access the parent instance of an object?

E.g. in method $object->subobject->Method() I want to access $object->var. Is it possible with something like $parent? I know it doesn't exist but is there similar way to do so?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

$object->subobject->Method() doesn't make object the parent of $subobject.
You can pass $object-var as parameter of the method call.
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Post by MarK (CZ) »

I've maybe used a bad word, not parent like in the way of extending the class but it is just higher in the hierarchy..
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

debug_backtrace() can tell you what class, and possibly a reference to the containing object from within the enclosed class' method.. but it should probably be given to the enclosed class in some fashion.. Although this is bad OOP behavior.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Code: Select all

class Object {
    var $children = array();
    var $parent = null;

    function newChild() {
        $child = new Object();
        $child->parent = $this;
        $this->children[] = $child;
        return $child;
    }

    function getParent() {
        return $thils->parent;
    }
}
(#10850)
Post Reply