note 100379 rejected from language.oop5.visibility by frozenfire

From: Date: Fri, 15 Jul 2011 02:20:01 +0000
Subject: note 100379 rejected from language.oop5.visibility by frozenfire
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-181542@lists.php.net to get a copy of this message
Note Submitter: ryan dot m dot lange at gmail dot com ---- 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.

« previous php.notes (#181542) next »