note 100931 added to language.oop5.visibility
| From: | Craig at osu1 dot php dot net | Date: | Tue, 16 Nov 2010 09:47:38 +0000 |
| Subject: | note 100931 added to language.oop5.visibility | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-174059@lists.php.net to get a copy of this message | ||
This is in response to ryan lange below. Your example given is not a "quirk" or bug, and
behaves as expected.
To try explain what's happening, do the following at the end of your example:
<?php
echo "<pre>";
print_r($child2);
echo "</pre>";
?>
This outputs:
ChildClass2 Object
(
[foo:private] =>
[foo] => bar
)
This demonstrates that ChildClass2 has created a member "foo" with the value
"bar" on the fly (ChildClass2::foo, rather than setting ParentClass::foo).
The reason for this is because of the private visibility given to ParentClass::foo. The docs state:
"Members declared as private may only be accessed by the class that defines the member."
This means ChildClass2 in your example never accesses the member defined in the parent class. Since
$this->foo doesn't exist to ChildClass2, it created it.
ParentClass, on the other hand, only ever accesses its local member, because it's unaware of
the existence of ChildClass2::foo.
If you want the behaviour of accessing members across in a class and its children, then you should
use the protected keyword, not private.
----
Server IP: 69.147.83.197
Probable Submitter: 87.81.122.105
----
Manual Page -- http://www.php.net/manual/en/language.oop5.visibility.php
Edit -- https://master.php.net/note/edit/100931
Del: integrated -- https://master.php.net/note/delete/100931/integrated
Del: useless -- https://master.php.net/note/delete/100931/useless
Del: bad code -- https://master.php.net/note/delete/100931/bad+code
Del: spam -- https://master.php.net/note/delete/100931/spam
Del: non-english -- https://master.php.net/note/delete/100931/non-english
Del: in docs -- https://master.php.net/note/delete/100931/in+docs
Del: other reasons-- https://master.php.net/note/delete/100931
Reject -- https://master.php.net/note/reject/100931
Search -- https://master.php.net/manage/user-notes.php