What is 'final' in PHP?

Asked

Viewed 1,519 times

4

I was at Github and I came across a class this way:

final class Noticia
{
    [...]
}

What use is this final reserved word?

-Can only be used in classes?

3 answers

12


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

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

Example of class that cannot subclasses:

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

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

Returns:

Class b may not inherit from final class (A)

Example of method that cannot be overwritten:

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

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

Returns:

Cannot override final method A::f()

6

This command serves to prevent the class from being inherited. It must be the final use class.

Example, you create a class called pessoa, from it you inherit and create pessoa_fisica and pessoa_juridica, in these two endings, you would put final class for example, a class derived from pessoa_fisica or pessoa_juridica. If you try to inherit, you will receive an error.

According to the documentation, see a definition of classes that would receive an error:

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

?>

3

It’s a way of saying this class can’t 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 even think that the standard should be the class not inheritable.

It can be used in method also saying that it cannot be overwritten when in inheritable classes. What should also be the default, although one of the reasons that is performance makes no difference in PHP.

Documentation. These codes make a mistake:

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 {
}

I put in the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.