PHP 8.4: property hooks and the end of the getter/setter ceremony

PHP 8.4 released November 21st. Property hooks are the feature. Everything else, and there’s quite a bit of it, is secondary. Property hooks For twenty years, if you wanted behavior on property access in PHP you had to write getters and setters: class User { private string $_name; public function getName(): string { return $this->_name; } public function setName(string $name): void { $this->_name = strtoupper($name); } } PHP 8.4 adds hooks directly on the property: class User { public string $name { set(string $name) { $this->name = strtoupper($name); } } } You can define get and set hooks independently. A property with only a get hook is computed on access: ...

January 5, 2025 · 7 min · Guillaume Delré