PHP 8.4 has been available since November 2024. It brings property hooks – a native implementation of getters and setters without boilerplate, asymmetric property visibility (public private(set)), new array functions and deprecation of functions untouched since PHP 4. This evolution lets you write more concise code without giving up encapsulation.
Property hooks – native getters and setters
<?php
declare(strict_types=1);
// PHP 8.4: property hooks - logic directly on the property
class OrderItem84
{
public float $price {
set(float $value) {
if ($value < 0) throw new \InvalidArgumentException('Price cannot be negative');
$this->price = $value;
}
get => $this->price;
}
public int $qty {
set(int $value) {
if ($value < 1) throw new \InvalidArgumentException('Qty must be positive');
$this->qty = $value;
}
}
// get-only hook - virtual computed property
public float $total {
get => $this->price * $this->qty;
}
}
$item = new OrderItem84();
$item->price = 99.99; // triggers set hook
$item->qty = 3;
echo $item->total; // triggers get hook: 299.97
// $item->total = 100; // Fatal error: property with only get is readonly
Asymmetric property visibility
<?php
declare(strict_types=1);
// PHP 8.4: public property that only the class can modify
class Product
{
public private(set) string $sku;
public private(set) float $price;
public protected(set) int $stock; // only class and children can set
public function __construct(string $sku, float $price, int $stock)
{
$this->sku = $sku;
$this->price = $price;
$this->stock = $stock;
}
public function applyDiscount(float $percent): void
{
$this->price = $this->price * (1 - $percent / 100); // OK - inside class
}
}
$product = new Product('SKU-001', 100.0, 50);
echo $product->sku; // OK - public read
echo $product->price; // OK - public read
$product->price = 50; // Fatal error: Cannot modify private(set) property
$product->applyDiscount(10); // OK - inside class
echo $product->price; // 90.0
New array functions – array_find() and friends
<?php
declare(strict_types=1);
$orders = [
['id' => 1, 'status' => 'complete', 'total' => 150.0],
['id' => 2, 'status' => 'pending', 'total' => 89.99],
['id' => 3, 'status' => 'processing', 'total' => 230.0],
['id' => 4, 'status' => 'pending', 'total' => 45.0],
];
// PHP 8.4: array_find() - first element satisfying condition
$firstPending = array_find($orders, fn($o) => $o['status'] === 'pending');
// ['id' => 2, 'status' => 'pending', 'total' => 89.99]
// PHP 8.4: array_find_key() - key of first matching element
$key = array_find_key($orders, fn($o) => $o['total'] > 200);
// 2 (index of order with total 230)
// PHP 8.4: array_any() - whether any element satisfies condition
$hasPending = array_any($orders, fn($o) => $o['status'] === 'pending');
// true
// PHP 8.4: array_all() - whether all elements satisfy condition
$allComplete = array_all($orders, fn($o) => $o['status'] === 'complete');
// false
Deprecations and breaking changes
<?php
// PHP 8.4 deprecates implicit nullable - you must be explicit
// PHP 8.3 (worked without warning):
function oldStyle(string $name = null): void {}
// PHP 8.4 (deprecated - throws E_DEPRECATED):
// function oldStyle(string $name = null): void {}
// PHP 8.4 (correct):
function newStyle(?string $name = null): void {}
// or:
function newStyle2(string|null $name = null): void {}
// Scan your code with:
// vendor/bin/phpstan analyse --level=6 app/code/
// or rector:
// vendor/bin/rector process app/code/ --config rector.php
Summary
PHP 8.4 property hooks eliminate the need to write getters and setters while preserving full encapsulation. Asymmetric visibility (public private(set)) allows properties that are publicly readable but privately writable – without extra code. New array functions fill the gap previously closed with array_filter and foreach. Before upgrading check for implicit nullable – it is the most common issue when migrating.
