PHP tips
5 tips · newest first
Modern PHP tips for 8.3/8.4: property hooks, asymmetric visibility, typed constants — language features that make code safer with less ceremony.
Prefer asymmetric visibility over readonly when a class mutates its own state
PHPpublic private(set) (PHP 8.4) makes a property readable from anywhere but writable only inside the class. readonly forb…
Chain methods directly off constructors in PHP 8.4
PHPPHP 8.4 allows new Foo()->method() without wrapping the instantiation in parentheses. It's a small ergonomic win that r…
Add #[\Override] in PHP 8.3 to catch broken method overrides at compile time
PHP#[\Override] (PHP 8.3) tells the engine you intend to override a parent or interface method. If no matching method exis…
Use property hooks in PHP 8.4 to compute and validate without getter methods
PHPPHP 8.4 lets a property define get/set hooks inline, so computed and validated values no longer need a separate method…
Type your class constants in PHP 8.3 to lock the contract across subclasses
PHPPHP 8.3 added type declarations for class, interface, and enum constants. Without a type, a subclass could override a c…