an image related to programming containing css php js html texts

PHP 8.4 Property Hooks: Writing Cleaner Boilerplate-Free Classes

The End of Getter and Setter Overload

For years, PHP developers have followed the standard practice of making class properties private and exposing them via public getters and setters. While this ensures encapsulation, it often leads to bloated classes filled with repetitive boilerplate code. With the release of PHP 8.4, property hooks have arrived to solve this problem by allowing you to define logic directly within the property declaration.

What Are Property Hooks?

Property hooks allow you to intercept the reading and writing of a property. Instead of writing a separate getFullName() method, you can attach a get hook directly to a $fullName property. This keeps related logic bundled together and makes your classes much easier to read.

Consider the traditional way of handling a simple user name formatting:

class User {
    private string $name;

    public function __construct(string $name) {
        $this->name = $name;
    }

    public function getName(): string {
        return ucfirst($this->name);
    }

    public function setName(string $name): void {
        $this->name = trim($name);
    }
}

The PHP 8.4 Approach

With property hooks, the same logic becomes much more concise. You define the get and set operations inside the property itself. This eliminates the need for private backing variables and separate methods.

class User {
    public string $name {
        set => trim($value);
        get => ucfirst($this->name);
    }

    public function __construct(string $name) {
        $this->name = $name;
    }
}

In this example, the $value variable is automatically available in the set hook, representing the value being assigned. The get hook returns the modified value whenever the property is accessed.

Virtual Properties

One of the most powerful features of hooks is the ability to create "virtual" properties. These are properties that don't actually store a value in memory but are calculated on the fly. This is perfect for derived data like a user's full name or an order total.

class Product {
    public function __construct(
        public float $price,
        public float $taxRate
    ) {}

    public float $totalPrice {
        get => $this->price * (1 + $this->taxRate);
    }
}

Here, totalPrice behaves like a standard property, but it is always calculated based on the current price and tax rate. This prevents data desynchronization bugs that often occur when manually updating dependent fields.

Why This Matters for Performance and DX

Property hooks aren't just about saving keystrokes. They improve the Developer Experience (DX) by making the intent of your code clearer. Tools like static analyzers and IDEs can better understand the relationship between data and its transformation logic. Furthermore, because these hooks are built into the engine, they are generally more efficient than manual method calls for simple transformations.

As PHP continues to evolve, the move towards a more declarative style of programming makes the language faster and more enjoyable to work with. If you are starting a new project on PHP 8.4, start leveraging property hooks to keep your DTOs and Entities lean and maintainable.