ZetCode

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.

final_class.php
<?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.

final_class_extend.php
<?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.

final_method.php
<?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.

final_method_override.php
<?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.

final_property.php
<?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.

final_constant.php
<?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.

final_interface.php
<?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.

Best practices

Final is most useful at the edges of a design, where an implementation is complete and extension would be a bug.

Source

PHP manual: final keyword

PHP manual: class constants

PHP manual: properties

PHP manual: object interfaces

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

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all PHP tutorials.