How do property hooks work in PHP 8.4?
Property hooks are a major new feature in `PHP 8.4`, allowing developers to define custom getter and setter logic directly within class property declarations. This eliminates the need for separate `getter` and `setter` methods, streamlining code and making property access more expressive and maintainable.
What Are Property Hooks?
Property hooks let you intercept and override the default behavior for reading from and writing to a property. Instead of using generic magic methods like `__get` and `__set` (which apply to all properties), property hooks provide fine-grained control over individual properties.
Defining Property Hooks
You can define `get` and/or `set` hooks for a property using curly braces after the property declaration. The get hook customizes what happens when the property is read, while the set hook customizes what happens when the property is assigned a value.
Example: Custom Getter and Setter
class User { public string $firstName { set(string $name) { $this->firstName = ucfirst($name); } } public string $lastName { set(string $name) => ucfirst($name); } public string $fullName { get => $this->firstName . ' ' . $this->lastName; set(string $name) { [$this->firstName, $this->lastName] = explode(' ', $name, 2); } } } $user = new User(firstName: 'ash', lastName: 'allen'); echo $user->fullName; // Outputs: Ash Allen
- The set hook for `firstName` ensures the first letter is `uppercase` whenever the property is set.
- The get hook for `fullName` dynamically combines `firstName` and `lastName` when accessed.
Syntax Variants
You can use either a multi-line block or a concise arrow function for hooks:
Block:
set(string $value) { $this->property = $value; }
Arrow:
set(string $value) => strtoupper($value);
Read-only and Write-only Properties
- If you only define a get hook, the property is effectively read-only.
- If you only define a set hook, the property is write-only.
The `__PROPERTY__` Magic Constant
Within a property hook, the new `__PROPERTY__` magic constant provides the name of the property being accessed. This can be useful for logging or generic logic, though you cannot use it to set the property value directly (doing so will trigger an error).
Property Hooks in Interfaces
You can declare property hooks in interfaces, enforcing that implementing classes provide the required `getter` and/or `setter` logic for those properties.
Summary Table
| Feature | Description | |---------------------------------|--------------------------------------------------------| | `get` hook | Custom logic when reading a property | | `set` hook | Custom logic when writing to a property | | Arrow/block syntax | Supports both concise and multi-line definitions | | Read-only/write-only support | Define only `get` or `set` for restricted access | | `__PROPERTY__` magic constant | Access property name inside hook | | Interface support | Require hooks in implementing classes |
Property hooks in `PHP 8.4` provide a modern, flexible way to manage property access and mutation, reducing boilerplate and aligning `PHP` with features found in other modern languages.
0%