New series: what assistive tech really hears on your checkout page. Start with part one →
Shipwright
Home
PHP Sep 18, 2026 2 min read 289 views

PHP 8.4 Property Hooks in Real Code

Tomás Herrera

Tomás Herrera

@tomasherrera_

Share

Replacing a page of getters and setters with hooks, and where we drew the line. PHP has quietly become a pleasant language to write. Typed properties, enums, readonly classes, and now property hooks remove a lot of the boilerplate that gave it a bad reputation.

Cover illustration for the article PHP 8.4 Property Hooks in Real Code
Figure 1. Replacing a page of getters and setters with hooks, and where we drew the line.

Modern PHP, minus the ceremony

We took a class from a six-year-old codebase and rewrote it with current language features. The result is half the length and, more importantly, the invalid states are no longer representable.

  • Use readonly properties for value objects.
  • Replace string constants with backed enums.
  • Commit a PHPStan baseline and shrink it over time.
  • Autoload everything through Composer.

Refactoring the old version

Static analysis turns those types into guarantees. Start at a low level with a baseline file, raise the level one step per sprint, and never let the baseline grow.

Adding manpower to a late software project makes it later.

Fred Brooks, The Mythical Man-Month, 1975

Quick takeaway

Make invalid states impossible and let the analyser prove it.

Static analysis catches the rest

🐘

Typed everything

Properties, parameters, and returns all declare types.

🔒

Readonly

Value objects cannot change after construction.

🧪

Analysed

PHPStan runs on every pull request.

None of this requires a framework. The examples run on plain PHP 8.3 or newer with Composer’s autoloader, and they translate directly to Laravel or Symfony projects.

If you try this on your own project, start with one page or one service rather than the whole codebase. A small experiment gives you real numbers to argue with, and it is easy to roll back if the idea does not fit.

We have been running this approach for several months now. The initial setup took about a day, and the ongoing cost is close to zero once the team knows where to look.

None of this is new, and that is the point. The techniques that last are usually the boring ones that are documented well and supported by the platform itself.

Wrapping up

As always, the examples are simplified to fit on a page. The production version has more error handling and logging, but the shape of the code is the same.

Tomás Herrera

Tomás Herrera

Author

PHP and WordPress developer for fifteen years. Tomás maintains a handful of open-source plugins and a strong opinion about hooks.

Related Posts

Comments 3

Diego Alvarez

Would this still apply on shared hosting without shell access?

Fatima Zahra

Solid write-up. The warning about caching bit us hard last year.

Tomás Herrera Author

Solid write-up. The warning about caching bit us hard last year.

Leave a comment

Your email will not be published. Sign in for a faster experience.

Supports **bold**, *italic*, and `code`.