All articles
PHPPHP 8.4LaravelModern PHPBackend Development

PHP 8.4 in Production: One Year On

PKParth Kanasagra
7 min read

PHP 8.4 released in November 2024. I migrated the first project over within a week (non-critical staging environment first, naturally) and have been running it in production for over a year now across three different applications. Here's my honest take.

Property Hooks: Actually Useful

The most-hyped feature, and it mostly lives up to it. Property hooks let you add getter/setter logic directly on a property without separate accessor methods:

class User
{
    // Computed from first + last name, no storage
    public string $fullName {
        get => "{$this->firstName} {$this->lastName}";
    }

    // Always store lowercase, trimmed
    public string $email {
        set(string $value) {
            $this->email = strtolower(trim($value));
        }
    }
}

$user->email = "  Test@EXAMPLE.com  ";
echo $user->email; // test@example.com

In Laravel, this overlaps with the existing Attribute casts API. I use property hooks in plain PHP value objects and DTOs, and still use Laravel's casts in Eloquent models where I want the ORM to handle it.

Asymmetric Visibility: More Useful Than It Sounds

Setting different visibility for reads and writes solves a real problem — you want a property readable everywhere but only writable from within the class:

class Order
{
    // Anyone can read status, only this class can change it
    public private(set) OrderStatus $status = OrderStatus::Pending;

    public function ship(): void
    {
        $this->status = OrderStatus::Shipped; // Works inside class
    }
}

$order = new Order();
echo $order->status->value;  // Works — public read
$order->status = OrderStatus::Shipped; // TypeError — private set

I use this in domain model classes. It enforces state transitions properly without needing to make the whole property private and writing explicit getter methods.

New Array Functions: Small But Genuinely Handy

Four new functions that I've actually used in real code:

// array_find — returns the first match, not just true/false
$admin = array_find($users, fn($u) => $u->isAdmin());

// array_find_key — returns the key of the first match
$nullKey = array_find_key($config, fn($v) => $v === null);

// array_any — short-circuit check if any element matches
if (array_any($roles, fn($r) => $r === 'superadmin')) {
    // ...
}

// array_all — all elements must match
$allVerified = array_all($users, fn($u) => $u->email_verified_at !== null);

array_find especially. I'd been writing collect($array)->first(fn...) which works but allocates a Collection object. The native function is leaner.

New HTML5 DOM API

The new Dom\HTMLDocument API handles HTML5 parsing properly without the charset issues that plagued old DOMDocument. If you're doing any HTML scraping or manipulation:

$doc = Dom\HTMLDocument::createFromString($html, LIBXML_NOERROR);
$title = $doc->querySelector('h1')?->textContent;

Much cleaner than the old way. I used this in a scraping project and it handled malformed HTML far better than DOMDocument.

The #[\Deprecated] Attribute

Mark your own code as deprecated with proper tooling support:

#[\Deprecated(
    message: 'Use UserService::createWithTeam() instead',
    since: '3.0'
)]
public function createUser(array $data): User
{
    // ...
}

When you call a deprecated method, you get a proper deprecation notice with the message and version. IDEs and static analysis tools pick it up too. This is small but it matters when maintaining large codebases.

What I Don't Use

The new BCMath object API — I haven't needed arbitrary precision math in the projects I've been on. The improved closure syntax from 8.1 was already covering my needs.

Should You Upgrade?

Yes. PHP 8.4 is stable, performant, and the migration from 8.2 or 8.3 is straightforward for most Laravel apps. Run php artisan about to check your current version, bump the requirement in composer.json, run your test suite, and you're mostly done. The features you'll actually use daily — property hooks, asymmetric visibility, the array functions — make the upgrade worth it.

Related Articles

PK

Parth Kanasagra

Certified Senior Laravel & PHP Developer · 9+ years experience · Available for hire