Asymmetric Visibility v2

Asymmetric visibility gives more fine-grained control over when a property can be read or written to. For example: you can make a property readable from everywhere, but only writable from within the private scope.

class Foo
{
    public private(set) string $bar = 'baz';
}
Click the bar to cast your vote!
36%
64%
12

This really makes the language complex.
The part with the interactions with the readonly keyword is really hard to make sense of.

I understand the subtle differences, don't get me wrong, it's just that I cannot understand them by just looking at the property definitions quickly. Imagine having to quickly make sense of a class with a dozen of properties like that. It's just nightmarish. I wouldn't accept a merge request that messy in my codebase.

I feel like if you need that much fine-grain control this is when having explicit getters/setters make sense.

Add property hooks on top of that and you have an explosion of complexity where you don't want: your domain logic is complex enough on it's own, you don't want to have properties become a maze.

Share:
nreynis avatar
nreynis
voted no
9

It certainly is a fancy feature, but it takes too much a toll on the cognitive load.

Right now visibility is straight-forward. In a glance I can map in my head the possible operations on a property when I see its visibility. Adding this to the language will mean there will be moments that I'll have to stop and work out exactly what it means I can do with a property.

Also, property hooks already solve these use cases: defining a virtual property hook with only a getter will prevent you from setting the property, and vice versa.

Share:
yogarine avatar
yogarine
voted no
6

I'm conflicted about this RFC. I definitely have use cases for "publicly readable and privately writable" properties. Especially since we still don't have clone with. I'm a afraid of the overlap between asymmetric visibility with readonly properties. By introducing asymmetric visibility, we make readonly properties essentially useless (asymmetric visibility can do the same, and more). I wonder how the community will deal with such a change: readonly properties were introduced in PHP 8.1, what happens if they become obsolete only a couple of versions later?

Despite my worry, I like this proposal on its own, and it would actually be useful to me in many cases. In the end, I'm inclined more towards voting yes than no.

Share:
Contributor brent avatar
brent
voted yes
4

I like the functionality this RFC provides and think this should come to PHP. But i don't like introducing a new keyword and syntax for this because it also increases the cognitive load. I would like the Property Hook approach that is discussed in the RFC more or using Attributes like the #[\SensitiveParameter]:

class Foo
{
	#[PrivateSet]
	public string $bar = 'baz';
}

or

class Foo
{
	#[Set(Private)]
	public string $bar = 'baz';
}

or some other Attribute. We should try to use existing syntax first.

Share:
thettler avatar
thettler
voted no
4

Its a nice concept but the proposed implementation will make defining visibility very complicated. Right now there are 3 visibility options, and its easy to understand what you can and cannot do when working with inheritance. Trying to understand the visibility hierarchy with this new RFC will take some time. https://wiki.php.net/rfc/asymmetric-visibility-v2#inheritance

Share:
ali avatar
ali
voted no
3

Asymmetric visibility allows for properties that are publicly readable but privately writable. This means that while the value can be accessed from anywhere, it can only be changed in controlled, intentional ways. By making a property writable only within a private scope, you can ensure that only specific methods or functions can modify it. This reduces the risk of unintended side effects from external code, which can potentially corrupt the state of the object.

Share:
faisal avatar
faisal
voted yes
3

It adds complexity, it can be confusing, but i think it's an important step forward for php to implement such feature.

For the readonly vs. asymmetric visibility discussion: you can mix them up since they aim a different goals (write-once vs writing from).

Share:
jorisvaesen avatar
jorisvaesen
voted yes
2

Don't like the syntax

Share:
yoshi129 avatar
yoshi129
voted no
2

For situations where you want to have a property be easily readable, but only modifiable in certain ways (e.g. a last-modified-date that should never be altered on its own, but which should be modified whenever an update/save method is run), this is quite useful.

The syntax is certainly not the prettiest, but the arguments in the RFC against using the alternative property-hook-entwined syntax are solid.

Share:
zeb avatar
zeb
voted yes
2

I like the idea behind this RFC, maybe the syntax is not my favourite, but it's ok.

Share:
rinodrummer avatar
rinodrummer
voted yes
2

Readonly properties only allow you to set a property in the constructor. Some times, you want to make your property public while only ensuring that it's only changeable within the confines of your class. This feature will make that possible

Share:
theoaksoft avatar
theoaksoft
voted yes
1

nice short syntax, usefull

Share:
pqr avatar
pqr
voted yes
1

Pretty good addition for the language that will allow to remove lots of boiler plate code

Share:
Contributor serhii avatar
serhii
voted yes
1

I think this syntax is not good. Just quickly scanning the code visually might give you a wrong impression if you just see the keyword private.
I prefer the syntax of kotlin a lot more which would look like this:

class Foo
{
    public string $bar = 'baz' {
		    private set;
				protected get
    }
}

For some reason this website messes with the markdown. Image the additional two lines for the setter and getter both to be indented one more time than the public keyword. So with a total of 8 spaces in front.

Share:
eydamos avatar
eydamos
voted no
1

Not elegant

Share:
liu avatar
liu
voted no

Check out another RFCs

new MyClass()->method() without parentheses

Chain method on newly created objects without parentheses

54
95 yes
16 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
264 no
Property Hooks

A new way of interacting with properties

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