note 102287 deleted from language.oop5.visibility by danbrown
| From: | danbrown@php.net | Date: | Mon, 07 Feb 2011 14:07:35 +0000 |
| Subject: | note 102287 deleted from language.oop5.visibility by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-176733@lists.php.net to get a copy of this message | ||
Note Submitter: michal dot php at glomesdal dot com
----
@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"
?>