note 50522 deleted from language.oop5.visibility by nlopess
| From: | nlopess@php.net | Date: | Wed, 02 Mar 2005 17:29:54 +0000 |
| Subject: | note 50522 deleted from language.oop5.visibility by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-85801@lists.php.net to get a copy of this message | ||
Note Submitter: opedroso at swoptimizer NOSPAM dot com
----
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
?>