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%
2

All arguments against already mentioned. I also believe internally PHP could become slower, because of more complex parsing. Copying some open questions from another argument too: How should child classes behave? Are they overridable? How do you know if there is a setter behavior defined already? How should traits behave? Is there a way to call the parent class setter/getter? Would that be overriden or just called by default?

Share:
mansurs avatar
mansurs
voted no
2

This is a much better approach than having to create getters and setters for validating values before assigning them to properties. Actually, this is an aspect from C# which I really like...

Share:
jhonatanjacinto avatar
jhonatanjacinto
voted yes
2

Getters and setters work just fine. Additionally RFC says there is a large and obscure performance penalty for array typed properties and it's introduces additional complexity.

Share:
stanislav avatar
stanislav
voted no
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

class User { private string $internalName; // Renamed to avoid conflict

public string $name {
    set {
        if (strlen($value) === 0) {
            throw new ValueError("Name must be non-empty");
        }
        $this->internalName = $value; // Store the value in internalName
    }
    get {
        return $this->internalName;
    }
}

public function __construct(string $name) {
    $this->name = $name;
}

}

The internal property used to store the value of $name is renamed to $internalName to avoid the conflict with the setter method, resolving the issue of the infinite loop. Error handling is improved by throwing a ValueError exception when an empty string is provided as the value for the name property. This ensures that validation errors are appropriately handled within the class.

Share:
mastermegamind avatar
mastermegamind
voted yes
1

This is well established in many other languages (Swift, Kotlin, C#) and the concerns of other commenters has not caused mass chaos as they would have you believe.

I'm not saying there aren't valid concerns, but the idea that "programmers will use it wrong" is not good enough.

Share:
christophercarranza avatar
christophercarranz...
voted yes
1

Late to the game here after it is all but guaranteed to pass, but I see this as a plus because it provides badly needed structure instead of magic __get() and __set() variables.

Share:
mikeschinkel avatar
mikeschinkel
voted yes
1

Looks like a good idea. first, I thought it will be too complicated for PHP to have this kind of syntax, but I've changed my mind, I think it will be amazing to have it in the language

Share:
Contributor serhii avatar
serhii
voted yes
1

use of the Value-Object seems better. "set" within a block of string doesn't look clean IMO.

Share:
junveloper avatar
junveloper
voted no
1

like it

Share:
parijke avatar
parijke
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

Check out another RFCs

new MyClass()->method() without parentheses

Chain method on newly created objects without parentheses

29
38 yes
13 no
The Pipe Operator

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

90
262 yes
122 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!