Property Hooks — get/set Without the Boilerplate
PHP 8.4's headline feature: property hooks let you define get and set behavior directly on a property declaration. No more writing boilerplate getter/setter methods. Properties can now have logic and still be accessed with $object->property syntax.
class User {
public string $fullName {
get => $this->firstName . ' ' . $this->lastName;
}
public string $email {
set(string $value) {
$this->email = strtolower(trim($value));
}
}
public function __construct(
public string $firstName,
public string $lastName,
public string $email,
) {}
}
$user = new User('Rasmus', 'Lerdorf', ' Rasmus@PHP.net ');
echo $user->fullName; // "Rasmus Lerdorf" — computed on access
echo $user->email; // "rasmus@php.net" — normalized on set
// Virtual properties — no backing storage needed
class Temperature {
public float $celsius {
get => ($this->fahrenheit - 32) / 1.8;
set(float $value) => $this->fahrenheit = $value * 1.8 + 32;
}
public function __construct(
public float $fahrenheit,
) {}
}
$t = new Temperature(212);
echo $t->celsius; // 100.0
$t->celsius = 0;
echo $t->fahrenheit; // 32.0
Property hooks work with interfaces (you can require a property to have a get or set hook), with readonly, and with constructor promotion. They're the biggest OOP addition since traits.
Significance: Paradigm Shift
Property hooks change the economics of PHP class design. You no longer need to choose between "public property (simple but no control)" and "private property + getter/setter (control but verbose)". You get both: clean property access syntax with full control over behavior. This eliminates thousands of lines of boilerplate in any OOP codebase.