PHP 8.4: Property Hooks
Tags: php, programming, news
Published: 2024-06-06 19:20:58

PHP 8.4 is expected for this fall. Let's review the RFC "Property Hooks."

## Disclaimer (08/06)

After reading a comment on this post, I think it should be indicated that the idea with this RFC is not to use it for anything and everything.

It's not meant to replace all cases, but can be very beneficial in case you need it (e.g., data object).

## What are PHP RFCs?

RFC means "Request For Comments." It's a pretty old concept (probably older than Internet itself) many core teams and their community use to discuss and implement new features, deprecate obsolete code or enhance existing structures.

The process for PHP is pretty well-documented, so do not hesitate to read [this page](https://wiki.php.net/rfc#request_for_comments) if you want more details.

Here we'll focus on a specific RFC that looks promising: Property Hooks.

## Other notable RFCs

While we'll focus on Property Hooks, there are other RFCs you might want to read:

- [Deprecate implicitly nullable parameter types](https://wiki.php.net/rfc/deprecate-implicitly-nullable-types)
- [new MyClass()->method() without parentheses](https://wiki.php.net/rfc/new_without_parentheses)
- [Increasing the default BCrypt cost](https://wiki.php.net/rfc/bcrypt_cost_2023)
- [Raising zero to the power of negative number](https://wiki.php.net/rfc/raising_zero_to_power_of_negative_number)
- [DOM HTML5 parsing and serialization](https://wiki.php.net/rfc/domdocument_html5_parser)
- [Multibyte for ucfirst and lcfirst functions](https://wiki.php.net/rfc/mb_ucfirst)
- [Multibyte for trim function mb_trim, mb_ltrim and mb_rtrim ](https://wiki.php.net/rfc/mb_trim)

## Where to find all accepted RFCs?

You can check [this page](https://wiki.php.net/rfc#php_84).

## Property hooks in short

This [RFC](https://wiki.php.net/rfc#request_for_comments) aims to remove the hassle of using boilerplate (e.g., getters/setters) for common interaction with object's properties.

PHP 8.0 already allows promoting properties in the constructor, so it's far less verbose than it used to be:

```PHP
class User 
{
    public function __construct(public string $name) {}
}
```

However, the RFC underlines the fact there's no built-in way to add custom behaviors or validation to these properties, which ultimately brings developers back to clumsy and verbose solutions (boilerplate or magic getters/setters).

With property hooks, this could be built-in the language:

```PHP
interface Named
{
    public string $fullName { get; } // make the hook required
}

class User implements Named
{
    public function __construct(private string $firstName, private string $lastName) {}

    public string $fullName {
        get => strtoupper($this->firstName) . " " . strtoupper($this->lastName);
    }
}
```

## What's the problem with getters and setters?

You may read this [old] introduction:

{% embed https://dev.to/spo0q/about-setters-and-getters-4l68 %}

👉🏻 When used blindly, setters and getters can break encapsulation, as  the idea is to prevent anybody from modifying the object from the outside, and you probably want to keep the implementation private.

## Wrap up

PHP contributors seem more and more inspired by other languages (e.g. [Kotlin](https://kotlinlang.org/docs/properties.html#getters-and-setters)).

The RFC only includes two hooks: `set` and `get`, but there could be more hooks in the future.