PHP 8.4 Property Hooks & Asymmetric Visibility in Production Laravel Apps
PHP 8.4 introduces property hooks and asymmetric visibility. Learn how this eliminates redundant getters, setters, and mutators in Laravel DTOs and Eloquent models.
PHP 8.4 represents one of the most substantial ergonomic upgrades to the language in recent history. By introducing Property Hooks (inspired by Kotlin and C#) and Asymmetric Visibility, PHP developers can write clean, fully encapsulated Data Transfer Objects and Domain Models without cluttering classes with boilerplate getter and setter methods.
Historically, enforcing invariants in PHP required declaring private properties paired with verbose public getter and setter methods. This added hundreds of lines of visual noise to data-heavy services and made simple refactoring tedious.
Encapsulated DTOs with Property Hooks
<?php
declare(strict_types=1);
namespace App\DTOs;
final class UserProfileDto
{
// Publicly readable, privately writable
public private(set) string $email;
public string $username {
set(string $value) => strtolower(trim($value));
get => '@' . $this->username;
}
public function __construct(string $email, string $username)
{
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new \InvalidArgumentException('Invalid email format');
}
$this->email = $email;
$this->username = $username;
}
}
Cleaner Eloquent Models and Invariant Checks
In Laravel 11 and modern PHP applications, property hooks provide a standard language-level replacement for custom magic accessors. Validation and formatting logic reside directly adjacent to property declarations rather than scattered across detached helper methods.
Asymmetric visibility (public private(set)) guarantees that consumers outside the class can freely read properties without fear of accidental external mutations, giving you immutable guarantees with zero getter boilerplate.
PHP continues to mature into a premier, strictly-typed language for enterprise web engineering.
Maintains the Syntaxis publishing and learning platform.