php version guide

PHP 8.4 Changes

PHP 8.4 added property hooks, asymmetric property visibility, lazy objects, #[\Deprecated], request_parse_body(), and several standard-library improvements. Property hooks are powerful but should not hide surprising side effects.

Changes Worth Recognising

  • Property hooks attach controlled get or set behaviour to properties.
  • Asymmetric visibility can expose reads while limiting writes.
  • Lazy objects are mainly infrastructure for frameworks and libraries.
  • #[\Deprecated] lets APIs signal migration paths to callers.

Representative Code

PHP example
<?php

final class Product
{
    public private(set) string $sku;

    public function __construct(string $sku)
    {
        $this->sku = strtoupper($sku);
    }
}

echo (new Product('php-84'))->sku . PHP_EOL;

// Prints:
// PHP-84

Upgrade Review

  • Keep property hooks small and unsurprising.
  • Check reflection, serialisation, hydrators, and framework support before adopting hooks broadly.
  • Do not build lazy objects manually in ordinary application code without a concrete need.

Use PHP 8.4 features where they simplify a real model; do not turn every getter and setter into a hook merely because syntax exists.

Practice

Restrict a Public Write

Create a class with a publicly readable but privately writable identifier using asymmetric visibility. Set it in the constructor and prove external code can read it.

Show solution

Declare public private(set) string $id;, assign it from class scope, and read it from the created object. An external reassignment should fail.