PHP 8.4 property hooks — typed accessors without the getter/setter boilerplate

Before PHP 8.4, if you wanted logic behind a property access you had two ugly choices: magic methods (__get/__set — slow, untyped, invisible to your IDE) or explicit getter()/setter() methods (verbose, and the “real” value lives in a private field). Property hooks (get/set) let a public, typed property carry logic while still behaving like a property.

Property Hooks in 30 Seconds

class User
{
    public string $name {
        get => ucwords($this->name);
        set => $this->name = strtolower(trim($value));
    }
}

$user = new User();
$user->name = "  ALICE ";
echo $user->name; // "Alice"

Two things to internalize:

  • The property still has a backing value. Inside the hook, $this->name reads/writes that backing value directly — no recursion, no infinite loop.
  • The set hook receives the assigned value as $value. You must assign it to the backing value yourself ($this->name = ...). If you don’t, the write is silently lost.

Virtual Properties (No Backing Value)

A property with only a get hook is virtual: there is no stored value, it is computed on every read. This is perfect for derived values.

class Rectangle
{
    public int $width;
    public int $height;

    public int $area {
        get => $this->width * $this->height;
    }

    public function __construct(int $width, int $height) {
        $this->width  = $width;
        $this->height = $height;
    }
}

$r = new Rectangle(4, 5);
echo $r->area; // 25

Because area has no set hook, it is implicitly readonly — assigning to it is a fatal error.

Validate on Write

The set hook is a natural home for invariants:

class Product
{
    public int $price {
        set {
            if ($value < 0) {
                throw new InvalidArgumentException("Price cannot be negative");
            }
            $this->price = $value;
        }
    }
}

$p = new Product();
$p->price = -5; // throws InvalidArgumentException

Gotchas

  • No default initializer. public string $name = 'x' { get => ... } is a fatal error. Set the default in the constructor instead.
  • set must assign the backing value. Forgetting $this->price = $value drops the data.
  • Hooks bypass magic methods. __get/__set are not invoked for a hooked property. Don’t mix the two patterns on the same class expecting them to compose.
  • Get-only (virtual) properties are readonly. You cannot assign to them.
  • Reflection sees them. ReflectionProperty::hasHook(ReflectionProperty::IS_GET) and getHook() expose the hook closures — useful for frameworks and serializers.

When to Use Hooks vs Explicit Methods

  • Use hooks for access/transformation that should feel like a property: normalization, computed values, and write-time validation ($user->name = ...).
  • Use explicit getX()/setX() when the operation is expensive, has side effects (caching, events, I/O), or returns a conceptually different thing than “the value of X”.

Takeaway

Property hooks remove the boilerplate of getters/setters and the fragility of __get/__set, while keeping fully typed, IDE-friendly, reflection-aware properties. Reaching for them instead of magic methods is one of the cleanest modernizations you can make to a PHP class.