note 102905 deleted from language.oop5.visibility by danbrown

From: Date: Mon, 14 Mar 2011 15:05:12 +0000
Subject: note 102905 deleted from language.oop5.visibility by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-177854@lists.php.net to get a copy of this message
Note Submitter: leoniu07 at gmail dot com ---- 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.

« previous php.notes (#177854) next »