PHP 8.4: property hooks, asymmetric visibility and what to check when upgrading
PHP 8.4 (released 21 November 2024) brought property hooks, public private(set) visibility, new without parentheses, lazy objects, an HTML5 DOM parser and array_find(). Plus the deprecations that matter.
PHP 8.4.0 was released on 21 November 2024. Active support ends on 31 December 2026; security fixes continue until 31 December 2028.
Property hooks
Properties can define get and set behaviour directly, replacing a lot of getter and setter boilerplate:
final class User
{
public string $email {
set(string $value) {
$this->email = strtolower(trim($value));
}
}
public string $displayName {
get => $this->firstName . ' ' . $this->lastName;
}
public function __construct(public string $firstName, public string $lastName, string $email)
{
$this->email = $email;
}
}
Interfaces can now declare properties, too, including which hooks they require.
Asymmetric visibility
Read publicly, write privately, without a getter:
final class Order
{
public private(set) string $status = 'new';
public function ship(): void
{
$this->status = 'shipped';
}
}
Smaller conveniences
newwithout parentheses in expressions:new Request()->withHeader('Accept', 'application/json').array_find(),array_find_key(),array_any()andarray_all().#[\Deprecated]for your own functions and methods, so callers get standard deprecation notices.- Lazy objects in core (
ReflectionClass::newLazyGhost()andnewLazyProxy()), used by ORMs and DI containers. Dom\HTMLDocument, a standards-compliant HTML5 parser, alongside the oldDOMDocument.BcMath\Numberobjects with operator overloading, andmb_trim(),mb_ucfirst(),mb_lcfirst().
What to check when upgrading
- Implicitly nullable parameters are deprecated.
function find(Criteria $c = null)must become?Criteria $c = null. This is the most common 8.4 notice and Rector fixes it automatically. trigger_error()withE_USER_ERRORis deprecated, as is theE_STRICTconstant.- The IMAP, OCI8, PDO_OCI and pspell extensions left core for PECL.
password_hash()withPASSWORD_BCRYPTnow defaults to cost 12 instead of 10: stronger, and slightly slower to hash.exit()anddie()became real functions; code that passed unusual values to them may behave differently.
See the full step-by-step list for your jump in the PHP upgrade checklist.
Sources
- PHP 8.4 release announcement, php.net