note 101210 added to language.oop5.visibility

From: Date: Thu, 02 Dec 2010 11:08:19 +0000
Subject: note 101210 added to language.oop5.visibility
Groups: php.notes 
Request: Send a blank email to php-notes+get-174556@lists.php.net to get a copy of this message
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". ---- Server IP: 69.147.83.197 Probable Submitter: 129.125.102.149 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.visibility.php Edit -- https://master.php.net/note/edit/101210 Del: integrated -- https://master.php.net/note/delete/101210/integrated Del: useless -- https://master.php.net/note/delete/101210/useless Del: bad code -- https://master.php.net/note/delete/101210/bad+code Del: spam -- https://master.php.net/note/delete/101210/spam Del: non-english -- https://master.php.net/note/delete/101210/non-english Del: in docs -- https://master.php.net/note/delete/101210/in+docs Del: other reasons-- https://master.php.net/note/delete/101210 Reject -- https://master.php.net/note/reject/101210 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#174556) next »