DeveloperJoy Logo DeveloperJoy

Property Hooks will be a reality in PHP 8.4

April 20, 2024
Property Hooks will be a reality in PHP 8.4

The Property Hooks RFC passed a significant milestone, getting an overwhelmingly positive 34 "yes" votes and only 1 "no" vote.

So it's approved and it will be released on PHP 8.4 version.

What are property hooks in PHP 8.4?

The proposal summary is clear.

Right now you can create public readonly properties and you don't need to create a custom set and get method. But, if you want to make a validation, or a transformation during the set method, for example, you need to declare the variable as private, and create both, set and get method.

This is not conveniente, so with this RFC we are introdución hooks for properties to override the behaviour of a property.

This property hooks are inspired by languages like Kotlin, C#, and Swift, and the syntax will be like this:

class User
{
    public function __construct(public string $first, public string $last) {}
 
    public string $fullName {
        get {
            return "$this->first $this->last";
        }
        set (string $value) {
            [$this->first, $this->last] = explode(' ', $value, 2);
        }
    }
 
}

And then you can use this property like this:

$u = new User('Larry', 'Garfield');
$u->fullName = 'Ilija Tovilo';
 
echo $u->first; // prints "Ilija"

← Go back to the blog

Work with me

Do you own a company or need help with your Laravel project? I can help you with that. Check the plans and let me know if you have any questions.

Get 1 month free with yearly plan during checkout.

Technical Advisor

$ 3 ,995

/m

What's included:

  • Lead your team to a better code architecture, testing mentality, clean code, and more.
  • Lead knowledge-sharing sessions depending on current company status and requirements.
  • Help with product and technical decisions.
  • Pause or cancel anytime.

Integrated Senior Developer

$ 5 ,995

/m

What's included:

  • Includes all Technical Advisor services.
  • Engages as a team member in daily tasks.
  • Participates actively in day-to-day development.
  • Direct communication:
    Slack, and meetings included.
  • Pause or cancel anytime.

Want to talk first?

Ok, just book a call now.

FAQ