note 87413 added to language.oop5.visibility
| From: | what at ever dot com | Date: | Thu, 04 Dec 2008 13:15:17 +0000 |
| Subject: | note 87413 added to language.oop5.visibility | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-147619@lists.php.net to get a copy of this message | ||
If you have problems with overriding private methods in extended classes, read this:)
The manual says that "Private limits visibility only to the class that defines the item".
That means extended children classes do not see the private methods of parent class and vice versa
also.
As a result, parents and children can have different implementations of the "same" private
methods, depending on where you call them (e.g. parent or child class instance). Why? Because
private methods are visible only for the class that defines them and the child class does not see
the parent's private methods. If the child doesn't see the parent's private methods,
the child can't override them. Scopes are different. In other words -- each class has a private
set of private variables that no-one else has access to.
A sample demonstrating the percularities of private methods when extending classes:
abstract class base {
public function inherited() {
$this->overridden();
}
private function overridden() {
echo 'base';
}
}
class child extends base {
private function overridden() {
echo 'child';
}
}
$test = new child();
$test->inherited();
Output will be "base".
If you want the inherited methods to use overridden functionality in extended classes but public
sounds too loose, use protected. That's what it is for:)
A sample that works as intended:
abstract class base {
public function inherited() {
$this->overridden();
}
protected function overridden() {
echo 'base';
}
}
class child extends base {
protected function overridden() {
echo 'child';
}
}
$test = new child();
$test->inherited();
Output will be "child".
----
Server IP: 195.222.16.155
Probable Submitter: ::ffff:90.190.131.122
----
Manual Page -- http://www.php.net/manual/en/language.oop5.visibility.php
Edit -- https://master.php.net/note/edit/87413
Del: integrated -- https://master.php.net/note/delete/87413/integrated
Del: useless -- https://master.php.net/note/delete/87413/useless
Del: bad code -- https://master.php.net/note/delete/87413/bad+code
Del: spam -- https://master.php.net/note/delete/87413/spam
Del: non-english -- https://master.php.net/note/delete/87413/non-english
Del: in docs -- https://master.php.net/note/delete/87413/in+docs
Del: other reasons-- https://master.php.net/note/delete/87413
Reject -- https://master.php.net/note/reject/87413
Search -- https://master.php.net/manage/user-notes.php