What it is and why to use it
A class author sometimes needs a method’s sequence to remain consistent in every subclass. final prevents an eligible method from being overridden. A final class cannot be extended at all. This makes extension boundaries explicit, but it does not make instances immutable or their public data automatically safe.
How it works
Place final before a class declaration to prohibit subclasses, or before a method declaration to protect that method while leaving the class extensible. A final method can still call a different overridable method, so its overall result can depend on the child. The examples use class and method forms available before PHP 8. Final class constants require PHP 8.1; final properties require PHP 8.4 and are separate features.
Common mistakes and limitations
Do not equate final with readonly or with thread safety. A final class can still expose methods that change its properties. Attempting to extend a final class or override a final method is an error, so invalid declarations are not included in these runnable examples. Consider interfaces or composition when consumers need variation.
Run the examples locally
Save each complete example in its own example.php file and run php example.php in a terminal from that folder. Each example is independent: do not paste both into one file, especially when they declare classes with the same names. Output appears in the terminal. The examples use PHP 8.0-compatible syntax; use a currently supported PHP release for development.
The <?php opening tag starts PHP code. declare(strict_types=1) requests strict scalar argument checks for function calls made from this file; it does not validate external input for you. echo writes output, and PHP_EOL adds the platform’s line ending. Quotation marks delimit strings; they are not part of the printed text unless explicitly included in the string.
These examples use built-in PHP language features and standard extensions. They need no Composer packages, database server, browser APIs or network access. Codectionary’s PHP preview does not execute PHP; use your local interpreter to reproduce the output.
Check your interpreter, lint and run one saved example
php -v
php -l example.php
php example.phpContinue learning
Use the related guides to connect this operation to the surrounding language features. Practise by changing the example inputs and predicting the output before running it.
Syntax
final class Identifier { /* no subclasses */ }Examples
Protect a method while exposing a small extension point
Keep message formatting fixed and let a child provide only the content.
<?php
declare(strict_types=1);
class Message {
final public function render(): string {
return '[' . $this->text() . ']';
}
protected function text(): string {
return 'Hello';
}
}
class WelcomeMessage extends Message {
protected function text(): string {
return 'Welcome, John';
}
}
echo (new WelcomeMessage())->render(), PHP_EOL;
- render is final, so subclasses cannot replace its bracket-formatting sequence.
- The protected text method remains an intentional extension point and is overridden by WelcomeMessage.
- The result uses the inherited render method with the child’s text. final on one method does not freeze every behaviour it calls.
- Expected output: [Welcome, John]
See that a final object can still change
Use a small final counter to distinguish inheritance from mutability.
<?php
declare(strict_types=1);
final class Counter {
private int $value = 0;
public function increment(): void {
$this->value++;
}
public function value(): int {
return $this->value;
}
}
$counter = new Counter();
$counter->increment();
echo $counter->value(), PHP_EOL;
- Counter cannot have subclasses because the class is final.
- Its increment method still changes the private value on the instance.
- The output is 1, demonstrating that final is an inheritance rule rather than an immutability guarantee.
- Expected output: 1
Best practices
- Document which extension points are intentionally supported.
- Distinguish extension restrictions from rules about changing object state.
Runtime & versions
PHP runtime on a server or CLI. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
PHP Manual