PHP 8.4 Is Here

The PHP core team officially released PHP 8.4 in November 2024, and it's one of the most feature-rich releases in recent memory. The update introduces significant language improvements that were long requested by the community — particularly around property handling and modern HTML parsing. Here's what you need to know before you upgrade.

Property Hooks: A Game Changer

Property hooks allow you to define get and set logic directly on class properties, eliminating the need for boilerplate getter/setter methods in many cases.

class User {
    public string $fullName {
        get => $this->firstName . ' ' . $this->lastName;
    }

    public string $email {
        set(string $value) {
            if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
                throw new ValueError("Invalid email address.");
            }
            $this->email = strtolower($value);
        }
    }

    public function __construct(
        public string $firstName,
        public string $lastName,
    ) {}
}

This dramatically reduces class verbosity for domain objects. Hooks are inherited, can be abstract, and work with interfaces — making them a first-class language feature rather than a workaround.

Asymmetric Visibility

PHP 8.4 allows you to set different visibility modifiers for reading and writing a property:

class Order {
    public private(set) int $itemCount = 0;

    public function addItem(): void {
        $this->itemCount++; // writable inside the class
    }
}

$order = new Order();
echo $order->itemCount; // readable publicly
$order->itemCount = 5;  // TypeError — write is private

This replaces the common pattern of a private property with a public getter method, making immutable-from-outside properties much simpler to express.

New HTML5-Compliant Parser

The ext-dom extension now includes an HTML5-compliant parser via the new Dom\HTMLDocument class, replacing the old DOMDocument::loadHTML() which was based on an aging libxml HTML parser.

$doc = Dom\HTMLDocument::createFromString(
    '<article><p>Hello <b>PHP 8.4</b></p></article>',
    LIBXML_NOERROR
);

$paragraphs = $doc->querySelectorAll('article p');
foreach ($paragraphs as $p) {
    echo $p->textContent; // "Hello PHP 8.4"
}

The new parser correctly handles modern HTML5 documents, including implicit tag closures and the full character set, which the old parser would frequently mangle.

New Array Functions

PHP 8.4 adds several useful array functions that reduce the need for verbose custom implementations:

  • array_find() — Returns the first element for which a callback returns true.
  • array_find_key() — Returns the key of the first matching element.
  • array_any() — Returns true if any element satisfies the callback.
  • array_all() — Returns true if all elements satisfy the callback.

Key Deprecations in PHP 8.4

  • Implicitly nullable parameter types (function foo(Type $val = null)) — use ?Type explicitly.
  • The old DOMDocument HTML parser methods emit deprecation notices when used with non-XML content.
  • Several SOAP extension behaviors related to encoding.

Upgrade Advice

PHP 8.4 follows the same upgrade path as recent major releases. Run composer outdated to check package compatibility, enable deprecation warnings in your test environment, and test against the new runtime before promoting to production. The PHP 8.4 migration guide on php.net covers every breaking change in detail.

This release rewards developers who adopt it — property hooks and asymmetric visibility alone will meaningfully clean up codebases built on domain-driven principles.