Skip to content
Leonidov.DEV

PHP 8.5 Property Hooks: 5 Rules I Teach by Example

Engineering
PHP 8.5 Property Hooks: 5 Rules I Teach by Example

Property hooks are easy to demo and easy to misuse. In code review I see the same five mistakes, so this is the lesson I give the teams I work with: five rules, each with the code that shows why the rule exists. The examples come from a micro framework with 728 tests, and they behave the same in any codebase.

Code: ATOM, GPL-3.0 licensed.

Rule 1: PHP 8.5 property hooks run on every read

A hook looks like a property and costs like a method, so the price of a method applies. This is a good hook, because it is arithmetic:

public string $method {
    get => strtoupper($this->raw['REQUEST_METHOD'] ?? 'GET');
}

This is a bad hook, and the reason is not style:

public array $headers {
    get => $this->parseHeaders($this->raw);
}

Every read parses the header string again. A loop over twelve headers calls it twelve times, and nothing in the calling code hints at the work. Before writing a hook, count the reads. If a hook does real work, memoise inside it so the property behaves like the field it replaced.

Rule 2: normalise in the set hook, once

Validation and cleanup scattered across call sites is how one email becomes three different strings in one table. Put the rule where the value enters the object:

public string $email {
    set (string $value) {
        $this->email = strtolower(trim($value));
    }
}

Every assignment now normalises, including the assignments written by a developer who never opened the model. That is the advantage a typed property has over a convention: the rule travels with the type.

Rule 3: choose one access path and keep it

Mixing $user->name and $user->getName() in one class teaches the next developer to guess. If the property is public and hooked, the getter has no job left; delete it. If the getter exists for an interface or a framework contract, keep the getter and do not add a hook that duplicates it.

One rule, one door. Two doors for the same value is how a codebase ends up with half a refactor.

Rule 4: no side effects in a hook

A get hook that writes a log line, increments a counter or lazy-loads a relation turns a read into an event. The page render then sends an email, and the trace starts in a template.

Reads should be cheap and repeatable. Writes belong in a method whose name says it writes. No compiler enforces this, which is exactly why it belongs in review.

Rule 5: hooks are not boundary validation

A set hook fires on assignment, which is not the same as validating untrusted input. A DTO built from $_POST still needs validation with an error message a person can read, and the database column still needs a constraint.

Hooks express invariants of an object that already holds sane data. They do not replace the check between a form and the table, and a set hook that throws is a hard way to learn that lesson.

Where hooks do not belong

Entities mapped to a wide table. Hooks on twenty columns add noise and hide the ORM’s own behaviour.

Anything with I/O, anything where the order of reads matters, and anything a debugger will struggle to show. Type three property names in the debugger and watch which ones fire: that is the test.

How to introduce them into an existing codebase

Start with the class that already has four one-line getters over the same field. Replace that field, run the suite, stop. Hooks spread by example, and a codebase where half the properties are hooked reads worse than either version on its own.

The same instinct applies beyond properties: keep one mechanism and make it readable. It is why the router in the same framework compiles the route table into a single PCRE pattern instead of walking a list, and why I keep the naming rules strict enough that a class name answers the first question in review.

If you want the reasoning behind that kind of decision, six architecture patterns covers the trade-offs, and correct naming in code covers the part reviewers notice first.

Background: the PHP 8.5 release notes and ATOM on GitHub, GPL-3.0 licensed.