note 109324 added to language.oop5.visibility
| From: | omega at 2093 dot es | Date: | Fri, 06 Jul 2012 12:04:54 +0000 |
| Subject: | note 109324 added to language.oop5.visibility | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-189050@lists.php.net to get a copy of this message | ||
This has already been noted here, but there was no clear example. Methods defined in a parent class
can NOT access private methods defined in a class which inherits from them. They can access
protected, though.
Example:
<?php
class ParentClass {
public function execute($method) {
$this->$method();
}
}
class ChildClass extends ParentClass {
private function privateMethod() {
echo "hi, i'm private";
}
protected function protectedMethod() {
echo "hi, i'm protected";
}
}
$object = new ChildClass();
$object->execute('protectedMethod');
$object->execute('privateMethod');
?>
Output:
hi, i'm protected
Fatal error: Call to private method ChildClass::privateMethod() from context 'ParentClass'
in index.php on line 6
In an early approach this may seem unwanted behaviour but it actually makes sense. Private can only
be accessed by the class which defines, neither parent nor children classes.
----
Server IP: 69.147.83.197
Probable Submitter: 79.159.146.135
----
Manual Page -- http://www.php.net/manual/en/language.oop5.visibility.php
Edit -- https://master.php.net/note/edit/109324
Del: integrated -- https://master.php.net/note/delete/109324/integrated
Del: useless -- https://master.php.net/note/delete/109324/useless
Del: bad code -- https://master.php.net/note/delete/109324/bad+code
Del: spam -- https://master.php.net/note/delete/109324/spam
Del: non-english -- https://master.php.net/note/delete/109324/non-english
Del: in docs -- https://master.php.net/note/delete/109324/in+docs
Del: other reasons-- https://master.php.net/note/delete/109324
Reject -- https://master.php.net/note/reject/109324
Search -- https://master.php.net/manage/user-notes.php