note 102287 added to language.oop5.visibility

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
@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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.