What’s New in PHP 8.4? Full Guide with Examples
What’s New in PHP 8.4? Full Guide with Examples
PHP 8.4 is one of those updates that doesn’t look like a total overhaul at first, but once you start coding, you realize it’s full of "finally!" moments. It’s less about reinventing the wheel and more about making the wheel much smoother to drive.
If you’re moving from PHP 8.3, this guide breaks down the actual game-changers, the syntax cleanup, and the "gotchas" that might trip up your existing projects.
1. Property Hooks (The Death of Getters & Setters)
This is the headliner. If you’re tired of writing boilerplate getName() and setName() methods just to trim a string or validate an email, Property Hooks are your new best friend. You can now define logic directly inside the property.
PHP
class User {
public string $name {
// Automatically capitalize when setting
set => ucfirst($value);
// Add a greeting when getting
get => "Hello, " . $this->name;
}
}
$user = new User();
$user->name = "gemini";
echo $user->name; // Output: Hello, Gemini
2. Asymmetric Visibility
Ever wanted a property to be publicly readable but privately writable? Before 8.4, you had to make the property private and write a public getter. Now, you can just say so in one line.
PHP
class BankAccount {
// Public to read, but only this class can change it
public private(set) float $balance = 0.0;
public function deposit(float $amount) {
$this->balance += $amount;
}
}
$account = new BankAccount();
echo $account->balance; // Works!
$account->balance = 5000; // Fatal Error: Cannot write private property
3. Chaining Without the Parentheses
This is a small "quality of life" tweak, but it makes your code look so much cleaner. Previously, if you wanted to instantiate a class and immediately call a method, you had to wrap it in parentheses. No more.
- Old way:
(new Logger())->log('info'); - New way:
new Logger()->log('info');
4. New "Find" Array Functions
Stop writing foreach loops just to find one item in an array. PHP 8.4 adds native functions that do exactly what you’ve been doing with array_filter or custom loops for years.
FunctionWhat it doesarray_find()Returns the first value that matches your criteria.array_find_key()Returns the first key that matches your criteria.array_any()Returns true if at least one item matches.array_all()Returns true if every item matches.PHP
$numbers = [1, 3, 5, 8, 10]; // Find the first even number $firstEven = array_find($numbers, fn($n) => $n % 2 === 0); // 8
5. The "Watch Out" List (Deprecations)
PHP 8.4 is pushing hard for better type safety. The biggest change you'll likely hit is the deprecation of implicit nullable types.
If you have a function like this:
PHP
function save(string $data = null) {}
PHP used to assume $data could be null because the default was null. Now, you must explicitly mark it with a ?.
- Fix:
function save(?string $data = null) {}