note 101311 deleted from language.oop5.visibility by danbrown
| From: | danbrown@php.net | Date: | Thu, 09 Dec 2010 15:07:20 +0000 |
| Subject: | note 101311 deleted from language.oop5.visibility by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-174743@lists.php.net to get a copy of this message | ||
Note Submitter: siamoen at gmail dot com
----
In response to: ryan dot m dot lange at gmail dot com
$child2 = new ChildClass2();
$child2->foo(); // <nothing>
$child2->bar(); // "bar"
The outputs of ChildClass2::foo() and ChildClass2::bar() are (completely) expected according to the
documentation because:
<?php
class ChildClass2 extends ParentClass
{
// You redefined constructor so the constructor of ParentClass is never called therefore the
private member $foo from ParentClass is never initialized and stays null.
public function __construct()
{
// If you want the foo work you have to call parent constructor manually:
parrent::__construct();
// This may be strange but you're in instance of ChildClass that does not know about
any foo data member as it is private in ParentClass so assignment like this just dynamically creates
new data member for this instance. It's specific for interpreted languages (ie. Python) but may
be interesting to find it in this documentation :)
$this->foo = 'bar';
}
public function bar()
{
// Accessing (ChildClass2)$this->foo private member set dynamically in constructor
echo $this->foo;
}
}
?>