downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Clonando objetos> <Métodos Mágicos
Last updated: Fri, 06 Nov 2009

view this page in

Palavra-Chave 'final'

PHP 5 introduz a palavra-chave 'final', que previne que classes filhas sobrecarreguem um método ou variável. Basta prefixar a definição com 'final'.

Exemplo #1 Exemplo de métodos 'final'

<?php
class ClasseBase {
   public function 
teste() {
       echo 
"ClasseBase::teste() chamado\n";
   }
   
   final public function 
maisTeste() {
       echo 
"ClasseBase::maisTeste() chamado\n";
   }
}

class 
ClasseFilha extends ClasseBase {
   public function 
maisTeste() {
       echo 
"ClasseFilha::maisTeste() called\n";
   }
}
// Resulta em erro Fatal: Não pode sobrescrever método final ClasseBase::maisTeste()
?>

Exemplo #2 Exemplo de classe Final

<?php
final class ClasseBase {
   public function 
teste() {
       echo 
"Método ClasseBase::teste() chamado\n";
   }

   
// Aqui não importa se você especificar a função como Final ou não
   
final public function maisTeste() {
       echo 
"Método ClasseBase::maisTeste() chamado\n";
   }
}

class 
ClasseFilha extends ClasseBase {
}
// Resulta em erro Fatal: A classe ClasseFilha não pode herdar de uma classe Final (ClasseBase)
?>


Clonando objetos> <Métodos Mágicos
Last updated: Fri, 06 Nov 2009
 
add a note add a note User Contributed Notes
Palavra-Chave 'final'
Kiam
31-Jul-2009 09:49
The final keyword doesn't change the visibility of a property / method, which is public by default.
jriddy at gmail dot com
17-Jul-2009 07:20
Note for Java developers: the 'final' keyword is not used for class constants in PHP. We use the keyword 'const'.

http://php.net/manual/en/language.oop5.constants.php
sunil dot boodram at gmail dot com
19-Mar-2009 03:54
Using the scope resolution operator, we can easily access constants, static variables, public functions and final functions from Final Classes

Note: even by omitting the "public" keyword from the "nightNews()" method, we can see that the function defaults to public even if prefixed with the keyword final.

<?php
final class news{
 
  const
micCheck = "test1"; // will work
 
static public $promptCheck = "test2"; //will work
 
  //will work
 
public function morningNews(){
    print
'good morning world!';
  }

 
//will work
 
final public function eveningNews(){
    print
'good evening world!';
  }

 
//will work
 
final function nightNews(){
    print
'good night world!';
  }

}

print
news::micCheck; // output: test1
print '<br/>';
print
news::$promptCheck; // output: test2
print '<br/>';
print
news::morningNews(); // output: good morning world!
print '<br/>';
print
news::eveningNews(); // output: good evening world!
print '<br/>';
print
news::nightNews(); // output: good night world!
?>
santoshjoshi2003 at yahoo dot co dot in
05-Dec-2008 08:45
The use of final keyword is just like that occurs in Java
In java final has three uses
    1) prevent class Inheritance
    2) prevent method overriding or redifination of
        method in subclass
    3) and to declare constants
 But the third point seems to be missing from the PHP
 I guess, as i am a java developer Currently gaining competence in PHP
slorenzo at clug dot org dot ve
31-Oct-2007 05:13
<?php
class parentClass {
    public function
someMethod() { }
}
class
childClass extends parentClass {
    public final function
someMethod() { } //override parent function
}

$class = new childClass;
$class->someMethod(); //call the override function in chield class
?>
penartur at yandex dot ru
22-Mar-2007 07:39
Note that you cannot ovverride final methods even if they are defined as private in parent class.
Thus, the following example:
<?php
class parentClass {
    final private function
someMethod() { }
}
class
childClass extends parentClass {
    private function
someMethod() { }
}
?>
dies with error "Fatal error: Cannot override final method parentClass::someMethod() in ***.php on line 7"

Such behaviour looks slight unexpected because in child class we cannot know, which private methods exists in a parent class and vice versa.

So, remember that if you defined a private final method, you cannot place method with the same name in child class.

Clonando objetos> <Métodos Mágicos
Last updated: Fri, 06 Nov 2009
 
 
show source | credits | sitemap | contact | advertising | mirror sites