note 101311 added to language.oop5.visibility
| From: | siamoen at gmail dot com | Date: | Thu, 09 Dec 2010 12:28:22 +0000 |
| Subject: | note 101311 added to language.oop5.visibility | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-174733@lists.php.net to get a copy of this message | ||
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;
}
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 193.85.150.3
----
Manual Page -- http://www.php.net/manual/en/language.oop5.visibility.php
Edit -- https://master.php.net/note/edit/101311
Del: integrated -- https://master.php.net/note/delete/101311/integrated
Del: useless -- https://master.php.net/note/delete/101311/useless
Del: bad code -- https://master.php.net/note/delete/101311/bad+code
Del: spam -- https://master.php.net/note/delete/101311/spam
Del: non-english -- https://master.php.net/note/delete/101311/non-english
Del: in docs -- https://master.php.net/note/delete/101311/in+docs
Del: other reasons-- https://master.php.net/note/delete/101311
Reject -- https://master.php.net/note/reject/101311
Search -- https://master.php.net/manage/user-notes.php