note 101210 deleted from language.oop5.visibility by danbrown

From: Date: Thu, 02 Dec 2010 14:14:15 +0000
Subject: note 101210 deleted from language.oop5.visibility by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-174562@lists.php.net to get a copy of this message
Note Submitter: A Nonny Bunny ---- In reply to the previous note (12-Oct-2010 03:05) First the reason why $child2->foo(); returns nothing. The ParentClass's construct function was never called, so the foo property was never initialized to "foo". ChildClass2's construct function should start by calling its parent class's construct function. public function __construct() { parent::__construct(); $this->foo = 'bar'; } Now $child2->foo() returns "foo", as expected. Next, the reason why $child2->bar() returns "bar". The statement $this->foo = 'bar'; creates a new public property with the name foo on the newly constructed ChildClass2 object. So, $child2->bar() uses $child2's public foo property (since it can't access the parent's private property), and returns "bar". And $child2->foo() uses $child2's parent's private foo property, and returns "foo".

« previous php.notes (#174562) next »