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!
58%
42%
2

It feels like it adds a huge amount of complexity just to avoid getter/setter methods. Also, I'm not sure it's a good thing to lose the explicit differentiation between a property access and method call.

Note: setter methods are something I almost never use in the first place, since it's typically better to have immutable value objects.

Share:
theodorejb avatar
theodorejb
voted no
2

This sounds pretty familiar to C# way for defining setters and getter. My problem with this RFC that it discourage code decoupling and separation of responsibilities. I know this is just an example use case, but an object shouldn't know how values should look like, it should be a responsibility of another component.

Share:
nabeel avatar
nabeel
voted no
1

Since I'm working also with Kotlin I desperatly crave for a feature like this. At least if it is as powerful as in Kotlin where I could e.g. change the setter to be protected or private while the getter can be public

Share:
eydamos avatar
eydamos
voted yes
1

Though I'd prefer only allowing the syntax with parentheses like set($value)

Share:
micheljonkman avatar
micheljonkman
voted yes
1

$value magically appearing is not good.

Share:
sakarikl avatar
sakarikl
voted no
1

I like it

Share:
indigoram89 avatar
indigoram89
voted yes
1

Makes it more readable.

Share:
eugen avatar
eugen
voted yes
1

Love it. like in swift. If property observers could be added this would be awesome!

Share:
grandfelix avatar
grandfelix
voted yes
1

I do believe this has merit and uses cases, but I agree that value objects would cover this better and there are other things the developers could put their time towards

Share:
nathan avatar
nathan
voted no
1

Provides a nice code structure keeping together: declaration, getter and guarded setter

Share:
rauljose avatar
rauljose
voted yes
1

This RFC needs a re-work. While I support the idea, I don't think this is the right approach. The fact that the RFC is so long is a massive red flag. For example there are 4 different ways to define hooks:

class User
{
    public string $username {
        set(string $value) {
            $this->username = strtolower($value);
        }
    }
}
class User
{
    public string $username {
        set(string $value) {
            $field = strtolower($value);
        }
    }
}
class User
{
    public string $username {
        set => strtolower($value);
    }
}
class User
{
    public function __construct(
        public string $username { set => strtolower($value); }
    ) {}
}

Why?!

This is not what PHP needs.

Share:
moebrowne avatar
moebrowne
voted no
1

I enjoy using set/get hooks in C# and strongly believe they are truly useful. Also, having them in interfaces is excellent!

Share:
baldie avatar
baldie
voted yes
1

Even if you won't use this yourselve, it will allow libraries and frameworks to do some really cool stuff. LGTM!

Share:
sandermuller avatar
sandermuller
voted yes
1

I've always envied C# developers for this feature in C#, I've been using __get() and __set() methods to achieve it

Share:
hsemix avatar
hsemix
voted yes
1

It's hard to read.

Share:
bYemma avatar
bYemma
voted no

Check out another RFCs

The Pipe Operator

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

89
260 yes
122 no
Interface Default Methods

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

94
169 yes
259 no
Short Closures 2.0

This RFC proposes a way to have multi-line short closures — closures that don't need explicit use statements, but can still have multiple lines and a return statement.

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