overloading variables in child classes

From: Date: Sat, 27 Oct 2001 19:12:09 +0000
Subject: overloading variables in child classes
Groups: php.general 
Request: Send a blank email to php-general+get-72577@lists.php.net to get a copy of this message
Hi all, I am a bit puzzled by the way PHP treates variables that have the same name in a base class and a child class. It seems to me that if a child class and its parent both have a variable with the same name, then they should be different variables. Instead, the example below indicates that they is only one variable : ---- class base { var $shared; function base() {$this->shared = "base";} function test() {echo("\$shared in base : " . $this->shared . "<br>\n");} } class child extends base { var $shared; function child(){$this->base(); $this->shared = "child";} function test() { echo ("\$shared in child : " . $this->shared . "<br>\n"); parent::test(); } } $the_child = new child(); $the_child->test(); ---- I would have expected the output to be $shared in child : child $shared in base : base instead I have $shared in child : child $shared in base : child Is there something I don't understand ? Regards, Alain Dresse adresse@brutele.be

« previous php.general (#72577) next »