Last November 26, 2020, the PHP 8 was released. PHP 8 was a significant milestone in the progress of PHP. It introduced major performance gains, new language features, and developer-centric improvements that make it easier than ever to write clean, secure, and efficient code.
Let’s break down the most important changes.
Performance Boost
PHP 8 also features the Just-In-Time (JIT) compiler that dramatically speeds up performance for CPU-bound work.
- Not always visible in standard web applications.
- Extremely useful for heavy calculations, scientific calculations, or machine learning tasks.
New Language Features
Union Types
You can now specify multiple possible types for a function parameter, property, or return type.
function calculateTotal(int|float $price): int|float {
return $price * 1.12;
}
Named Arguments
Arguments can be passed to functions by name instead of position, making code more readable.
function createUser(string $name, int $age, bool $active = true) { ... }
createUser(age: 25, name: "Ben");
Attributes (Annotations)
Rather than PHPDoc comments, you can now add structured metadata through attributes.
#[Route("/home")]
class HomeController { }
Constructor Property Promotion
You can declare and initialize class properties directly in the constructor.
class User {
public function __construct(
private string $name,
private int $age
) {}
}
Match Expression
A contemporary alternative to switch, shorter and safer.
$status = 200;
$message = match ($status) {
200 => 'OK',
404 => 'Not Found',
default => 'Unknown',
};
Nullsafe Operator
Avoid null errors with a safe operator.
$username = $user?->profile?->username;
Type System & Error Handling Enhancements
• Static return type: methods can return static for late static binding.
• Mixed type: a new type that can accept any value.
• Stricter type checks: more warnings and fatal errors rather than silent failures.
Deprecations and Removals
PHP 8 tidied up legacy features:
• Removed legacy, deprecated functions and extensions.
• Resources moved to objects (e.g., GD, Curl).
• Removed support for real, magic_quotes, and other legacy quirks.
Overall Impact
• Cleaner, more expressive syntax.
• Improved runtime performance.
• Tighter typing system.
• Simpler integration with contemporary frameworks (Laravel, Symfony, Drupal).
PHP 8 makes PHP faster, safer, and more developer-friendly, pushing it closer to modern programming languages in terms of features and performance.