note 50522 added to language.oop5.visibility

From: Date: Wed, 02 Mar 2005 17:09:50 +0000
Subject: note 50522 added to language.oop5.visibility
Groups: php.notes 
Request: Send a blank email to php-notes+get-85799@lists.php.net to get a copy of this message
It is important to notice that an inherited private method will only be called from methods in its own class. That is, if the call to an "inherited" private method comes from the base class, the base private method will be called. If the call comes from a method in the derived class, the "inherited" private method will be called. An example: <?php /** * Define MinhaClasse */ class MinhaClasse { // Contrutores devem ser public public function __construct() { } // Declara um método public public function MeuPublico() { } // Declara um método protected protected function MeuProtegido() { } // Declara um método private private function MeuPrivado() { echo "<br>Em MinhaClasse::MeuPrivado"; } // Esse é public function Foo() { $this->MeuPublico(); $this->MeuProtegido(); $this->MeuPrivado(); } } $minhaclasse = new MinhaClasse; $minhaclasse->MeuPublico(); // Funciona $minhaclasse->MeuProtegido(); // Erro Fatal $minhaclasse->MeuPrivado(); // Erro Fatal $minhaclasse->Foo(); // Public, Protected e Private funcionam /** * Define MinhaClasse2 */ class MinhaClasse2 extends MinhaClasse { private function MeuPrivado() { echo "<br>Em MinhaClasse2::MeuPrivado"; } // Esse é public function Foo2() { $this->MeuPublico(); $this->MeuProtegido(); $this->MeuPrivado(); } } $minhaclasse2 = new MinhaClasse2; $minhaclasse2->Foo(); // Funciona, mas a função MinhaClasse::MeuPrivado() será chamada // e não MinhaClasse2:MeuPrivado(), como se não tivesse sido redefinida! $minhaclasse2->Foo2(); // Public e Protected funcionam, Private não ?> ---- Manual Page -- http://www.php.net/manual/en/language.oop5.visibility.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+50522 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50522&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50522&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50522&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50522&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50522&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+50522&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#85799) next »