Re: OOP Get Parent data

From: Date: Thu, 11 Mar 2004 15:58:12 +0000
Subject: Re: OOP Get Parent data
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-180179@lists.php.net to get a copy of this message
Rolf van de Krol wrote:
class session { var $test="TEST"; var $browser; function init() { $this->browser = new browser; } } class browser { function parent_test() { return xxx; } } I tried $this->parent->test but it doesn't work. Can anybody help me?
There is no implicit reference created in an instance of a class to the instance of it's container for fairly obvious reasons when you think about it. The only way you can do what you want to do it to pass a reference to $this to the constructor of the browser class in the session::init method. Something like this... class session { var $test="TEST"; var $browser; function init() {
    $this->browser = new browser($this);
} } class browser { var $session = null; function browser(&$session) {
    $this->session = &$session;
} function parent_test() {
    return $this->session->test;
} } -- Stuart

« previous php.general (#180179) next »