note 100379 added to language.oop5.visibility

From: Date: Tue, 12 Oct 2010 15:05:54 +0000
Subject: note 100379 added to language.oop5.visibility
Groups: php.notes 
Request: Send a blank email to php-notes+get-173158@lists.php.net to get a copy of this message
Here's a bizarre little quirk that I haven't seen mentioned before: <?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]" $child2 = new ChildClass2(); $child2->foo(); // <nothing> $child2->bar(); // "bar" ?> The output of ChildClass1::foo() and ChildClass1::bar() are expected given the private visibility of $foo. However, the output of ChildClass2::foo() and ChildClass2::bar() are completely unexpected according to the documentation (to my understanding, at least). Setting a private property of a parent class in the constructor of a child class is not only possible, but essentially "hijacks" that property away from the parent class. ---- Server IP: 69.147.83.197 Probable Submitter: 24.131.221.37 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.visibility.php Edit -- https://master.php.net/note/edit/100379 Del: integrated -- https://master.php.net/note/delete/100379/integrated Del: useless -- https://master.php.net/note/delete/100379/useless Del: bad code -- https://master.php.net/note/delete/100379/bad+code Del: spam -- https://master.php.net/note/delete/100379/spam Del: non-english -- https://master.php.net/note/delete/100379/non-english Del: in docs -- https://master.php.net/note/delete/100379/in+docs Del: other reasons-- https://master.php.net/note/delete/100379 Reject -- https://master.php.net/note/reject/100379 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#173158) next »