note 102287 added to language.oop5.visibility

From: Date: Sun, 06 Feb 2011 20:46:55 +0000
Subject: note 102287 added to language.oop5.visibility
Groups: php.notes 
Request: Send a blank email to php-notes+get-176718@lists.php.net to get a copy of this message
@ryan dot m dot lange at gmail dot com The constructor in ChildClass2 overrides the constructor in the parent. Now, ParentClass must be initialized by calling its constructor explicitly from ChildClass2. ChildClass2 has no visibility to $foo in the parent so it has locally created its own $foo. Since it was undeclared, it is a public property. <?php class ParentClass { private $foo; public function __construct() { $this->foo = 'foo'; } public function foo() { echo $this->foo . "\n"; } } class ChildClass2 extends ParentClass { public function __construct() { $this->foo = 'bar'; parent::__construct(); // added this line } public function bar() { echo $this->foo . "\n"; } } $child2 = new ChildClass2(); $child2->foo(); // now returns "foo" $child2->bar(); // "bar" echo $child2->foo; // "bar" ?> ---- Server IP: 69.147.83.197 Probable Submitter: 99.172.160.139 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.visibility.php Edit -- https://master.php.net/note/edit/102287 Del: integrated -- https://master.php.net/note/delete/102287/integrated Del: useless -- https://master.php.net/note/delete/102287/useless Del: bad code -- https://master.php.net/note/delete/102287/bad+code Del: spam -- https://master.php.net/note/delete/102287/spam Del: non-english -- https://master.php.net/note/delete/102287/non-english Del: in docs -- https://master.php.net/note/delete/102287/in+docs Del: other reasons-- https://master.php.net/note/delete/102287 Reject -- https://master.php.net/note/reject/102287 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#176718) next »