note 102905 added to language.oop5.visibility

From: Date: Mon, 14 Mar 2011 14:56:47 +0000
Subject: note 102905 added to language.oop5.visibility
Groups: php.notes 
Request: Send a blank email to php-notes+get-177853@lists.php.net to get a copy of this message
My 2 cents to "ryan dot m dot lange at gmail dot com" <?php class ParentClass { private $foo; public function __construct() { $this->foo = 'foo'; } public function foo() { echo $this->foo; } } class ChildClass1 extends ParentClass { public function bar() { echo $this->foo; } } class ChildClass2 extends ParentClass { public function __construct() { $this->foo = 'bar'; } public function bar() { echo $this->foo; } } $parent = new ParentClass(); $parent->foo(); // "foo" $child1 = new ChildClass1(); $child1->foo(); // "foo" $child1->bar(); // "Notice: Undefined property: ChildClass1::$foo in [file] on line [line]" //because, $child1 can't access private properties of the parent class and the variable "foo" is not defined in it's own class either $child2 = new ChildClass2(); $child2->foo(); // <nothing>, because there is a constructor defined in class ChildClass2, the parent constructor won't be called. The private variable "foo" is not assigned a value as a result and keeps null. $child2->bar(); // "bar", because the variable "foo" has been defined and assigned a value within class ChildClass2's constructor, "$this->foo = 'bar';". The variable "foo" only belongs to ChildClass2 and doesn't override that "foo" in it's parent class. ?> Hope my comments help. ---- Server IP: 69.147.83.197 Probable Submitter: 74.216.223.199 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.visibility.php Edit -- https://master.php.net/note/edit/102905 Del: integrated -- https://master.php.net/note/delete/102905/integrated Del: useless -- https://master.php.net/note/delete/102905/useless Del: bad code -- https://master.php.net/note/delete/102905/bad+code Del: spam -- https://master.php.net/note/delete/102905/spam Del: non-english -- https://master.php.net/note/delete/102905/non-english Del: in docs -- https://master.php.net/note/delete/102905/in+docs Del: other reasons-- https://master.php.net/note/delete/102905 Reject -- https://master.php.net/note/reject/102905 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#177853) next »