PHP final Keyword
last modified November 25, 2026
The PHP final keyword prevents a class from being extended, or a method, property, or constant from being overridden in a subclass. It turns an implementation into a closed contract.
This article covers final classes, methods, properties, and constants, the errors raised by a subclass that ignores the lock, the rules for interfaces and private members, and the design cases where final is the right choice.
Basic definitions
final closes a class, method, property, or constant to further inheritance. A final class cannot be extended; a final method cannot be overridden; a final property cannot be redeclared in a subclass; a final constant cannot be redeclared either.
The keyword goes before the visibility modifier: final public function total(), final protected string $id, final public const VERSION. It applies to the declaration, not to the value: a final class can still be instantiated, its methods can still be called, and its properties can still change unless they are also readonly.
Final is a design decision, not a security boundary. It says that the implementation is complete and that a subclass would break an invariant. The extends keyword article covers inheritance itself, and readonly covers properties that cannot change after construction.
A final class
The first example declares a small value object as final. The class has a private property, a constructor, and one method that formats the value.
<?php
declare(strict_types=1);
final class Money
{
public function __construct(private int $cents)
{
}
public function format(): string
{
return number_format($this->cents / 100, 2) . " EUR";
}
}
$price = new Money(1999);
echo $price->format(), "\n";
The class is final, but nothing else changes: the constructor runs, format is callable, and the object behaves like any other. The restriction appears only when another class tries to inherit from it.
$ php final_class.php 19.99 EUR
Extending a final class fails
A subclass of a final class is rejected when the child is declared, before any object is created.
<?php
declare(strict_types=1);
final class Money
{
}
class Euro extends Money
{
}
PHP stops at compile time with Class Euro cannot extend final class Money. If variation is needed, wrap the final class in another class or extract an interface, rather than weakening the lock. Composition keeps the final class intact and still lets callers depend on an abstraction.
$ php final_class_extend.php
Fatal error: Class Euro cannot extend final class Money in /app/final_class_extend.php on line 9
Stack trace:
#0 {main}
A final method
A final method can be inherited and called normally. The keyword only forbids replacing its body in a subclass.
<?php
declare(strict_types=1);
class Order
{
final public function total(): int
{
return 100;
}
}
class DiscountedOrder extends Order
{
public function totalWithDiscount(): int
{
return $this->total() - 10;
}
}
$order = new DiscountedOrder();
echo $order->total(), "\n";
echo $order->totalWithDiscount(), "\n";
The final method is inherited and called from the child, and the child adds a new method that uses it. Non-final methods in the parent can still be overridden, which lets a class protect one step while leaving the others open.
$ php final_method.php 100 90
Overriding a final method fails
The child may add methods, but it may not declare one with the same name as a final parent method.
<?php
declare(strict_types=1);
class Order
{
final public function total(): int
{
return 100;
}
}
class DiscountedOrder extends Order
{
public function total(): int
{
return 90;
}
}
The error is raised when DiscountedOrder is declared, not when total is called: Cannot override final method Order::total(). The message names the parent method, so the fix is either to remove the override or to remove final from the parent.
$ php final_method_override.php
Fatal error: Cannot override final method Order::total() in /app/final_method_override.php on line 15
Stack trace:
#0 {main}
Final properties
PHP 8.4 added final properties. They work like other properties, but a subclass cannot redeclare them.
<?php
declare(strict_types=1);
class Base
{
final protected string $id = "base";
}
class Child extends Base
{
protected string $id = "child";
}
The error is Cannot override final property Base::$id. A final property can still be read and written by the code that is allowed to see it, so it is not a constant; it only guarantees that no subclass redeclares it. A private property cannot be final, because a private member is never visible in a subclass. A private(set) property is implicitly final, which the constructor property promotions article covers together with asymmetric visibility.
$ php final_property.php
Fatal error: Cannot override final property Base::$id in /app/final_property.php on line 10
Stack trace:
#0 {main}
Final constants
PHP 8.1 allows class constants to be final. Reading the constant works as usual; the restriction applies to a subclass that tries to replace it.
<?php
declare(strict_types=1);
class Config
{
final public const VERSION = "1.0";
}
class AppConfig extends Config
{
public const VERSION = "2.0";
}
The message is AppConfig::VERSION cannot override final constant Config::VERSION. Interface constants can also be final, so an implementing class cannot change the value. The const keyword article covers constants, interfaces, and the final const form.
$ php final_constant.php
Fatal error: AppConfig::VERSION cannot override final constant Config::VERSION in /app/final_constant.php on line 10
Stack trace:
#0 {main}
Final and interfaces
An interface method has no body, so it cannot be final. A class that implements an interface can be final, and then the whole implementation is closed to inheritance.
<?php
declare(strict_types=1);
interface Cache
{
final public function get(string $key): string;
}
PHP reports Interface method Cache::get() must not be final. The method is a contract, and every implementing class writes the body; there is no inherited implementation to protect. Mark the implementing class final instead when the implementation itself should not be extended. The interface keyword and implements keyword articles cover the contract.
$ php final_interface.php
Fatal error: Interface method Cache::get() must not be final in /app/final_interface.php on line 7
Stack trace:
#0 {main}
Rules and limitations
A few combinations are worth knowing before reaching for the keyword.
- A final class does not need final methods; they are already protected from overriding.
finalcannot be combined withprivateon a property; PHP reportsProperty cannot be both final and private.- A private method marked
finaltriggers a warning, because private methods are never overridden. - A final method can be inherited and called; only overriding is forbidden.
- Final does not make a class immutable. Use
readonlyfor that, and do not confuse the two.
Best practices
Final is most useful at the edges of a design, where an implementation is complete and extension would be a bug.
- Use final for value objects, small leaf classes, and classes whose invariants depend on the exact implementation.
- Do not mark every class final by default; libraries often need extension points.
- Document why a class or method is final, because the keyword does not show the reason.
- Prefer composition over inheritance when a final class needs variation.
- Remember that final classes are harder to mock in tests; design test seams with interfaces.
- Use final properties and constants for invariants and configuration, not as a general default.
Source
This article covered the PHP final keyword: final classes, methods, properties, and constants, the errors raised by an override, the interface restriction, and the cases where a closed implementation is the right design. Continue with PHP extends keyword for inheritance, or read PHP abstract keyword and PHP readonly keyword for the neighboring modifiers.
Author
List all PHP tutorials.