Goodbye Boilerplate: Using Property Hooks in Modern PHP
Theodoros Kafantaris
Published on December 06, 2025
For over a decade, PHP developers have written the same boring code: a private property, a getX() method, and a setX() method. It clutters your classes and makes them hard to read.
With modern PHP, Property Hooks allow you to define this logic directly on the property itself.
The Old Way:
class User {
private string $name;
public function getName(): string {
return ucfirst($this->name);
}
public function setName(string $name): void {
if (strlen($name) === 0) throw new ValueError("Name cannot be empty");
$this->name = $name;
}
}
The Modern Way:
class User {
public string $name {
get => ucfirst($this->value);
set {
if (strlen($value) === 0) throw new ValueError("Name cannot be empty");
$this->value = $value;
}
}
}
Why use it? It keeps related logic together. Validation and formatting happen right where the data lives, not 50 lines further down the file.