PHP 8.4 Highlights: Property Hooks, Asymmetric Visibility, New Array Functions
PHP 8.4, released on November 21 2024, introduces Property Hooks for inline getter/setter logic, asymmetric visibility allowing different read/write access levels, native lazy‑object support, bracket‑less instantiation, an HTML5‑capable DOM class, and four new array utilities (array_find, array_find_key, array_any, array_all) alongside numerous other enhancements.
Property Hooks
Property Hooks are a major addition in PHP 8.4, inspired by Kotlin, C#, Swift, JavaScript and Python. They allow defining getter and setter logic directly on a property.
<?php
declare(strict_types=1);
class Foo
{
private string $id;
public function getId(): string
{
return $this->id;
}
public function setId(string $id): void
{
$this->id = $id;
}
}With constructor property promotion, the same class can be written more concisely:
<?php
class Foo
{
function __construct(public string $id) {}
}Property Hooks also let you keep explicit getters/setters while adding custom logic, for example:
<?php
class Foo
{
public function __construct(
public string $id {
get => '#' . $this->id;
set(string $id) => $this->id = mb_strtoupper($id);
}
) {}
}They can be combined with interfaces to enforce a contract:
<?php
interface HasId
{
public string $id { get; set; }
}
class Foo implements HasId
{
function __construct(
public string $id {
get => '#' . $this->id;
set(string $id) => $this->id = mb_strtoupper($id);
}
) {}
}Asymmetric Visibility for Class Properties
Asymmetric visibility lets a property have different visibility for reading (public) and writing (protected or private). The rules require typed properties, apply only to object properties, and the setter cannot be broader than the getter.
<?php
class Foo
{
function __construct(
private string $id,
) {}
public function getId(): string
{
return $this->id;
}
}
class Bar
{
function __construct(
public private(set) string $id,
) {}
}Native Support for Lazy Objects
PHP 8.4 adds native support for lazy objects (ghosts and proxies) that defer instantiation until the object is actually needed, useful in frameworks such as Doctrine and Symfony.
Bracket‑less Object Instantiation
Parentheses after new are optional, simplifying chained method calls:
<?php
$o = new Operation(0)->add(10)->multiply(2);HTML5 Parsing
A new DOM\HTMLDocument class provides HTML5 parsing while keeping backward compatibility with the existing HTML4 API, built on the Lexbor library.
New Array Functions
Four new functions operate on arrays: array_find – returns the first element matching a callback. array_find_key – returns the key of the first matching element. array_any – returns true if any element matches. array_all – returns true only if all elements match.
Examples:
<?php
$array = ['A', 'AA', 'AAA'];
array_find($array, static fn(string $v): bool => strlen($v) > 2); // 'AAA'
array_any($array, static fn(string $v): bool => strlen($v) > 2); // trueOther Improvements and Bug Fixes
Highlights include new multibyte string functions (mb_trim, mb_ltrim, mb_rtrim, mb_ucfirst, mb_lcfirst), DateTime creation from timestamps, removal of exit / die in favor of special functions, deprecation of several PECL extensions, new rounding modes, added Sodium encryption algorithms, OpenSSL updates, and new cURL options. Full changelog:
https://www.php.net/ChangeLog-8.phpSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
