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.
Table of contents:
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.
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.
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.
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.
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.
$ 3 ,995
/m
What's included:
$ 5 ,995
/m
What's included:
Ok, just book a call now.
Good question! For starters, the annual cost of a full-time senior-level developer now exceeds $100,000, plus benefits (and good luck finding one available).
Aside from that, you may not always have enough work to keep them busy at all times, so you're stuck paying for time you aren't able to utilize.
With the monthly plan, you can pause and resume your subscription as often as you need to ensure you're only paying your developer when you have work available for them.
No, once subscribed you're able to request as many things as you'd like, and they will be delivered one by one.
Delivery will always be fast without compromising quality. Very complex requests will be divided into smaller products for continuous delivery.
Always, before starting to work, we can make a planning so that you know, before starting, when each change will be delivered.
You'll work directly with me, the founder and only person behind DeveloperJoy.
We understand you may not have enough work to fill up entire month. Perhaps you only have one or two requests at the moment. That's where pausing your subscription comes in handy.
Billing cycles are based on 31 day period. Let's say you sign up and use the service for 21 days, and then decide to pause your subscription. This means that the billing cycle will be paused and you'll have 10 days of service remaining to be used anytime in the future
I had experience making products with PHP, Go, and JavaScript/TypeScript.
Also I feel really comfortable working with WordPress, Laravel, Symfony, Vue.js, React, Svelte, and more.
Mail me and we will see how best to collaborate.
You can make this request by email, sharing a task board with me or, depending on your plan, by call or Slack.
That's fine. You can pause your subscription when finished and return when you have additional requests needs. There's no need to let the remainder of your subscription go to waste.
Due to the high quality nature of the work, there will be no refunds issued.