Property Hooks

Developers often use methods to wrap and guard access to object properties. There are several highly common patterns for such logic, which in practice may be verbose to implement repeatedly. Alternatively, developers may use __get and __set to intercept reads and writes generically, but that is a sledge-hammer approach that intercepts all undefined (and some defined) properties unconditionally. Property hooks provide a more targeted, purpose-built tool for common property interactions.

class User 
{
    public string $name {
        set {
            if (strlen($value) === 0) {
                throw new ValueError("Name must be non-empty");
            }
            $this->name = $value;
        }
    }
 
    public function __construct(string $name) {
        $this->name = $name;
    }
}

You can play around with them on 3v4l.org

Click the bar to cast your vote!
61%
39%
1

This is good to have

Share:
esternwinluck avatar
esternwinluck
voted yes
1

i don't see any use case that can not be solved better with a value object.

i rather validate the whole object than properties by itself.

Share:
gertvdb avatar
gertvdb
voted no
1
Share:
undjike avatar
undjike
voted yes
1
Share:
anonym-uz avatar
anonym-uz
voted no
1

I want it!

Share:
theofidry avatar
theofidry
voted yes
1

good

Share:
liu avatar
liu
voted yes
1

Even though this is a nice syntax that we also know from other programming languages, I think it is problematic. The reason for this lies in the ambiguity: How does the developer know whether $entity->setSlug() = '...' does something different than $entity->slug?

Without property hooks the case is quite clear: the setter method could still contain some "under-the-hood" steps (such as validation), while directly changing the property sets the value "unfiltered". It should stay that way.

Share:
dsentker avatar
dsentker
voted no
1

I like the idea - getting away from the mess that __get and __set can introduce is a great goal, but this example is confusing and appears to be incomplete. I checked the RFC for more details, and the number of different forms this could take just made me even more confused. Why do we need multiple variants and combinations of shorthand and magic variables, when it's ultimately just defining a special use case function? I'd be much happier with a simple keyword added to a normal function definition to denote it's purpose.

Share:
valorin avatar
valorin
voted no
1

OK

Share:
lloricode avatar
lloricode
voted yes
1

This is one of the reasons why I love C#. Would be a nice addition in PHP!

Share:
woutercypers avatar
woutercypers
voted yes
1

Nice to have!

We already have this kind of properties in C#!

Share:
pentiminax avatar
pentiminax
voted yes
1

It simplifies dedicated logic for setters/getters, without having to mess directly with __get() and __set().

Share:
rinodrummer avatar
rinodrummer
voted yes
1

This would be a very interesting way to ensure values of an property.

Share:
tiago avatar
tiago
voted yes
1

This seems a really nice addition! Love it!

Share:
aplouro avatar
aplouro
voted yes
1

This is long overdue.

Share:
jeffdafoe avatar
jeffdafoe
voted yes

Check out another RFCs

new MyClass()->method() without parentheses

Chain method on newly created objects without parentheses

29
38 yes
13 no
Interface Default Methods

Interface Default Methods improves backwards compatibility when changing interfaces, but also add a way to have multi-inheritance in PHP.

95
168 yes
263 no
The Pipe Operator

The "pipe operator" |> allows you to chain multiple function calls in a more convenient way.

90
262 yes
122 no
RSS Feed Contribute Watch on YouTube Our License
© 2024 RFC Vote. This project is open source. Contribute and collaborate with us!