What is a final class in php?

The final keyword prevents child classes from overriding a method or constant by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.

Example #1 Final methods example

Example #2 Final class example

Example #3 Final constants example as of PHP 8.1.0

Note: Properties cannot be declared final: only classes, methods, and constants [as of PHP 8.1.0] may be declared as final. As of PHP 8.0.0, private methods may not be declared final except for the constructor.

penartur at yandex dot ru

15 years ago

Note that you cannot ovverride final methods even if they are defined as private in parent class.
Thus, the following example:

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.

someone dot else at elsewhere dot net

8 years ago

@thomas at somewhere dot com

The 'final' keyword is extremely useful.  Inheritance is also useful, but can be abused and becomes problematic in large applications.  If you ever come across a finalized class or method that you wish to extend, write a decorator instead.

mattsch at gmail dot com

8 years ago

You can use final methods to replace class constants.  The reason for this is you cannot unit test a class constant used in another class in isolation because you cannot mock a constant.   Final methods allow you to have the same functionality as a constant while keeping your code loosely coupled.

Tight coupling example [bad to use constants]:



Loose coupling example [eliminated constant usage]:

cottton at i-stats dot net

8 years ago

imo good to know:

santoshjoshi2003 at yahoo dot co dot in

13 years ago

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

Anonymous

11 years ago

The behaviour of FINAL is not as serious as you may think. A little explample:


Normally you would expect some of the following will happen:
- An error that final and private keyword cannot be used together
- No error as the private visibility says, that a method/var/etc. is only visible within the same class

But what happens is PHP is a little curios: "Cannot override final method A::method[]"

So its possible to deny method names in subclasses! Don't know if this is  a good behavior, but maybe its useful for your purpose.

Baldurien

12 years ago

"Note for Java developers: the 'final' keyword is not used for class constants in PHP. We use the keyword 'const'."

//php.net/manual/en/language.oop5.constants.php

This is more or less true, regardless of the fact that constant [being defined at class level or not] in PHP are only scalar [int, string, etc] while in Java they may be pure object [ex: java.awat.Color.BLACK]. The only possible solution of having such kind of constant is :


That said, perhaps it is useless unless PHP automatically calls the __init[] method.

However, one alternative that could be done in certain case is this :



This can only work when one class is defined per file, since we are assured that __autoload[] will be called to load the file containing the class.

eg:

test2.php:

test.php:

slorenzo at clug dot org dot ve

14 years ago

t at bestcodepractise dot com

8 years ago

@someone
@thomas
Decorating a finalized class is not possible. The decorator that's mentioned is incomplete. There's a fundamental flaw in it. Look:

What you've created is just an object that happens to have the same methods[a duck type]. But if in the client code someone makes decision based on the type of your passed decorator they'll make incorrect decision - or to be more precise not the one that you, the author of the 'decorator' wants them to make.
FYI that's the correct implementation based on GoF:


I haven't come across any legitimate use of a finalizing class/method and I personally think that 'final' is has no much use and is just a copy'n'pasted from Java into PHP. The keyword makes code difficult to test. If you have to create a test double from a finalized class because you'll need create a derived type to shadow the methods you don't care about. If one of them is finalized you've already lost.

xavier dot inghels at gmail dot com

7 years ago

Final is not really about overriding or overloading a method.
PHP doesn't support [yet ?] method ovverriding.

The final keyword however prevent you to redefine a method already previously declared.

suisuiruyi at gmail dot com

6 years ago

Note: Properties cannot be declared final, only classes and methods may be declared as final.
you can use trait:

John smith

5 years ago

Right way:
final protected function example[] {
}

Wrong way:

protected final function example[] {
}

Source: Practices

What is a final class?

A final class is a class that can't be extended. Also methods could be declared as final to indicate that cannot be overridden by subclasses. Preventing the class from being subclassed could be particularly useful if you write APIs or libraries and want to avoid being extended to alter base behaviour.

What is final class explain with example?

The final modifier for finalizing the implementations of classes, methods, and variables. The main purpose of using a class being declared as final is to prevent the class from being subclassed. If a class is marked as final then no class can inherit any feature from the final class. You cannot extend a final class.

What is the meaning of a final class and a final method?

You use the final keyword in a method declaration to indicate that the method cannot be overridden by subclasses. The Object class does this—a number of its methods are final .

What is static and final in PHP?

final static declares a method which is static [can be called without an instance of the class] and final [can't be overridden by subclasses]. static alone can be used to define a class-scoped variable, which isn't constant [but variables can't be final ].

Chủ Đề