What is 'final' in PHP?

2

I was in Github and came across a class like this:

final class Noticia
{
    [...]
}

What is the purpose of this final reserved word?

Can only be used in classes?

    
asked by anonymous 07.08.2017 / 22:35

3 answers

7

The keyword final can only be used in classes and methods basically means that a class can not have any daughter (inheritance).

In methods it means that subclasses can not override / modify this method.

Example class that can not subclass:

 final class A{
    public function f(){
        echo 'f';
    }
 }

 class b extends a{
    public function x(){
        echo 'x';
    }
 }

Return:

  

Class b may not inherit from final class (A)

Method example that can not be overwritten:

class A{
    public final function f(){
        echo 'f';
    }
 }

 class b extends a{
    public function f(){
        echo 'minha implementação';
    }
 }

Return:

  

Can not override final method A :: f ()

    
07.08.2017 / 22:40
5

This command is used so that the class can not be inherited. It should be the end use class.

Example, you create a class called pessoa , from it you inherit and create pessoa_fisica and pessoa_juridica , in these two end, you would put final class so that for example, no class derived from pessoa_fisica or% with%. If you try to inherit, you will get an error.

According to documentation , see a class definition that would receive an error:

/ p>

<?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)

?>
    
07.08.2017 / 22:41
2

It's a way of saying that this class can not be inherited, which is usually a good idea. Developing classes prepared to be inherited is much more difficult, the next maintenance becomes a burden. I think even the default should be the class not inheritable.

Can be used in method also saying that it can not be overwritten when in inheritable classes. What should also be the default, though one of the reasons for performance makes no difference in PHP.

Documentation . These codes give error:

class BaseClass {
   public function test() {
       echo "BaseClass::test() called\n";
   }

   final public function moreTesting() {
       echo "BaseClass::moreTesting() called\n";
   }
}

class ChildClass extends BaseClass {
   public function moreTesting() {
       echo "ChildClass::moreTesting() called\n";
   }
}


final class BaseClass {
   public function test() {
       echo "BaseClass::test() called\n";
   }

   // Here it doesn't matter if you specify the function as final or not
   final public function moreTesting() {
       echo "BaseClass::moreTesting() called\n";
   }
}

class ChildClass extends BaseClass {
}
    
07.08.2017 / 22:40