When to use Final in PHP?

When to use Final in PHP?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Intent Behind final in PHP

The final keyword in PHP is often misunderstood as a way to lock down code that doesn't need extension, but its primary utility lies in architectural stability and explicit contract enforcement. When you declare a class as final, you are making a binary choice: this class will be the leaf node of your inheritance hierarchy. It signals to other developers—and to static analysis tools—that no child classes should exist for this entity.

Consider the snippet you provided:

<?php
final class Foo extends Bar {
    public function __construct() { // Corrected syntax
        echo 'John Doe';
    }
}

In your example, marking Foo as final prevents anyone from creating a NewFoo that extends Foo. This might seem restrictive, especially if you anticipate needing to add features later by extending the class. However, the decision usually stems from a specific design pattern or security requirement.

The most common scenario where final is essential is when a class implements an interface but the class itself should not be extended. For instance, if you are building a repository that adheres to a strict data access pattern, allowing extensions can break the encapsulation of that pattern. If Foo extends Bar, and Bar has methods relying on internal state or specific behavior that child classes might override in unexpected ways, extending Foo could inadvertently alter the contract established by Bar. By making Foo final, you ensure that any modification to its behavior must happen within the class itself, promoting immutability of its structural role.

Another critical use case involves performance optimization at the opcode level. The Zend Engine in PHP has specific optimizations for final classes. When a class is marked as final, the engine knows it will never be part of an inheritance chain where methods might be resolved dynamically through late static binding or virtual calls across multiple levels. This allows the JIT compiler (in supported versions) and the VM to generate more efficient machine code, bypassing some of the checks required for polymorphism. While the performance gain in application logic is negligible for most scripts, in high-frequency loops involving millions of object instantiations, this distinction can become measurable.

There is also the consideration of security and API stability. If a class is intended to be part of a public API or used as a base for a framework's core components, allowing extensions invites third-party code to break assumptions made by your application logic. For example, if Foo relies on Bar's implementation of a specific resource handling method, extending Foo allows a subclasser to override that behavior entirely. If that override changes the type of resources returned or alters error handling, it could cascade into failures in dependent services.

In frameworks like Laravel, you will often encounter final classes when defining core service providers or certain facade implementations. This is rarely about preventing simple feature addition; it is about protecting the integrity of the framework's internal mechanics. If a developer were to extend a final class from the vendor directory, they could inadvertently introduce bugs that bypass the framework's own safety checks.

Ultimately, you should use final when you have determined that extending the class provides no architectural benefit and only introduces risk. If your current codebase requires adding new functionality via extension, then Foo should not be final. But if Foo represents a complete implementation of a domain concept where inheritance would violate separation of concerns or expose internal state, final is the correct tool to enforce that boundary.

Our network