DeveloperJoy Logo DeveloperJoy

Everything about PHP 8.4 Property Hooks

June 15, 2024
Everything about PHP 8.4 Property Hooks

The upcoming PHP version 8.4 introduces a groundbreaking feature known as property hooks. These hooks, inspired by languages such as Kotlin, C#, and Swift, provide a new methodology for handling object properties, aiming to reduce boilerplate code and enhance code readability.

What Are Property Hooks?

Property hooks allow developers to attach custom logic to property access and mutation operations. With property hooks, the traditionally cumbersome task of writing getter and setter methods for class properties becomes more streamlined. PHP 8.4 provides two syntax variants for property hooks, reminiscent of short and multi-line closures.

Simplifying Code

Using property hooks, you can override the get and set actions for a property with arbitrary logic. Here is an example that demonstrates this feature:

class User implements Named
{
    private bool $isModified = false;
 
    public function __construct(private string $first, private string $last) {}
 
    public string $fullName {
        // Override the "read" action with arbitrary logic.
        get => $this->first . " " . $this->last;
 
        // Override the "write" action with arbitrary logic.
        set { 
            [$this->first, $this->last] = explode(' ', $value, 2);
            $this->isModified = true;
        }
    }
}

In this example, the fullName property is enhanced with custom behavior for both reading and writing operations. When fullName is read, it concatenates the first and last properties. Similarly, when fullName is written to, it parses the value and updates both first and last, while also marking the object as modified.

Enhancing Interfaces

Moreover, property hooks can be defined in interfaces, ensuring that implementing classes adhere to specific property access requirements:

interface Named
{
    // Objects implementing this interface must have a readable $fullName property.
    public string $fullName { get; }
}

By stipulating that a class implementing the Named interface must have a readable fullName property, interfaces can enforce consistency across varied implementations.

Benefits of Property Hooks

  • Reduced Boilerplate Code: Eliminate the need for repetitive getter and setter methods.
  • Enhanced Readability: Inline the property logic directly where it is used, making the code more intuitive.
  • Consistency: Define access rules in interfaces that multiple classes can implement cohesively.
  • Flexibility: Introduce complex property behaviors without complicating the class structure.

PHP 8.4's introduction of property hooks marks a significant step forward in the evolution of the language. By simplifying property management, enhancing code readability, and ensuring interface consistency, property hooks provide developers with powerful tools to write cleaner and more maintainable code.

For more detailed information, refer to the official PHP RFC page on property hooks.

← 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

Not available

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