You've seen the class. Two private properties, four public methods, and three of those methods do nothing except pass a value in or out. The one that matters, the setter that tidies the value up, is buried in the middle.
PHP 8.4 added property hooks, which let you put that logic on the property itself. Less code to read, and the rule lives where the value lives.
What a hook is
A property can now have a get hook, a set hook, or both. Reading the property runs get. Writing to it runs set. Callers just use $object->property, the same as any public property.
The PHP 8.4 release announcement shows it with a small locale class:
```php // Example from the PHP release notes (PHP 8.4) class Locale { public string $languageCode;
public string $countryCode { set (string $countryCode) { $this->countryCode = strtoupper($countryCode); } }
public string $combinedCode { get => \sprintf("%s_%s", $this->languageCode, $this->countryCode); set (string $value) { [$this->languageCode, $this->countryCode] = explode('_', $value, 2); } }
public function __construct(string $languageCode, string $countryCode) { $this->languageCode = $languageCode; $this->countryCode = $countryCode; } }
$brazilianPortuguese = new Locale('pt', 'br'); var_dump($brazilianPortuguese->countryCode); // BR var_dump($brazilianPortuguese->combinedCode); // pt_BR ```
Two things to notice. $countryCode stores a value, and its set hook upper-cases it on the way in. $combinedCode never stores anything of its own: it's worked out from the other two each time you read it.
Backed and virtual properties
The PHP manual splits hooked properties into two kinds:
- Backed: at least one hook refers to the property itself (
$this->countryCodeinside thecountryCodehooks). There's real storage behind it. - Virtual: no hook refers to the property itself. Nothing is stored; the hooks calculate or redirect. A virtual property with only a
gethook is effectively read-only.
A validation example
Here's the kind of class I'd use hooks for in a small business app: an email address that's always tidied and checked.
```php // Sketch based on the PHP 8.4 property hooks rules (untested) final class Customer { public string $email { set (string $value) { $value = strtolower(trim($value)); if (filter_var($value, FILTER_VALIDATE_EMAIL) === false) { throw new InvalidArgumentException('Not a valid email address'); } $this->email = $value; } }
public string $emailDomain { get => substr($this->email, strpos($this->email, '@') + 1); }
public function __construct(string $email) { $this->email = $email; // goes through the set hook too } } ```
The assignment in the constructor runs the set hook as well, so there's no back door where an untidy email slips in. $emailDomain is virtual, so it can never drift out of step with $email.
The gotchas
- No readonly. The manual is clear that hooks and
readonlydon't mix. If you want "anyone can read, only the class can write", PHP 8.4's asymmetric visibility (public private(set) string $email;) is the tool for that. - Array properties. Writing to an element, as in
$cart->items[] = $line;, involves a reference, which would skip asethook. On a backed property that's only allowed when the property has nothing but a&gethook. In practice, give the class anaddItem()method instead. - Interfaces can ask for them. An interface can now declare
public string $email { get; }, so you can require a readable property rather than agetEmail()method.
When a method is still better
A property read should feel cheap and boring. If a get hook would run a database query, call an API or change other state, keep it as a method. People reading $order->total don't expect it to hit the network, and they're right not to.
In other words: hooks for tidying, checking and simple calculated values. Methods for actions.
Why this makes money sense
Less boilerplate means less code to read, review and get wrong. Putting the rule on the property means every write goes through it, so bad data is stopped at the door instead of being cleaned up later on someone's time.
The full rules are in the PHP manual's property hooks page and the PHP 8.4 release announcement.
Deeper PHP habits live in PHP Done The Correct Way (paperback on Amazon UK, Kindle UK, free companion code on the PHP AIR code page). More practical notes are on the PHP AIR blog, or start at phpair.co.uk.