# [RFC] Asymmetric visibility

- Thread: https://externals.io/message/118353
- Original archive: https://news-web.php.net/php.internals/118353
- Started by Larry Garfield on 2022-08-05T17:08:31+00:00
- Messages: 45
- RFC: Asymmetric visibility (Declined) — https://externals.io/rfc/asymmetric-visibility.md
- Page 1 of 2: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/118353.md?page=2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #118353 — Larry Garfield — 2022-08-05T17:08:31+00:00

URL: https://externals.io/message/118353#118353

Ilija Tovilo and I are happy to present the first new RFC for PHP 8.3: Asymmetric Visibility.

https://wiki.php.net/rfc/asymmetric-visibility

Details are in the RFC, but it's largely a copy of Swift's support for the same.

-- 
  Larry Garfield
  larry@garfieldtech.com

---

## #118354 — Thomas Nunninger — 2022-08-05T17:39:58+00:00

URL: https://externals.io/message/118353#118354 · In reply to: #118353

Hi,
Am 05.08.22 um 19:08 schrieb Larry Garfield:
> Ilija Tovilo and I are happy to present the first new RFC for PHP 8.3: Asymmetric Visibility.
> 
> https://wiki.php.net/rfc/asymmetric-visibility
> 
> Details are in the RFC, but it's largely a copy of Swift's support for the same.

Great to hear that there is a new and simple proposals.

One note: In the example about references, I assume the class Baz must 
extend from Foo.

Good luck with this proposal
Thomas

---

## #118355 — Matthew Weier O'Phinney — 2022-08-05T18:08:48+00:00

URL: https://externals.io/message/118353#118355 · In reply to: #118353

> Ilija Tovilo and I are happy to present the first new RFC for PHP 8.3:
> Asymmetric Visibility.
>
> https://wiki.php.net/rfc/asymmetric-visibility
>
> Details are in the RFC, but it's largely a copy of Swift's support for the
> same.
>

I have two comments:

- For reflection purposes, having two separate methods feels like it will
be cumbersome; you'd need to check both to determine if you'd need to make
the reflection property accessible before changing the value. An
isPublicSet() would alleviate that, or potentially a getSetFlags() method
against which you could apply a bit mask. You'd need to add a constant for
public set I both cases.

- The number of items that appear to the left of a property is growing,
making understanding a declaration increasingly difficult, and your
discussion of future scope indicates that this is likely to get worse. I'm
wondering if this sort of behavior could be indicated via attributes
instead? Something like `#[PropertySetBehavior(PROPERTY_SET_PRIVATE)]`.
Attributes have the benefit of being separate from the property
declaration, arguably more readable (one per line), and composable. This
might play into some of the future scope items as well.

Overall, though, love the design simplicity!


> --
>   Larry Garfield
>   larry@garfieldtech.com
>
> --
> 
> To unsubscribe, visit: https://www.php.net/unsub.php

---

## #118356 — michal.brzuchalski@gmail.com — 2022-08-05T19:27:49+00:00

URL: https://externals.io/message/118353#118356 · In reply to: #118353

Hi Larry,

pt., 5 sie 2022, 19:09 użytkownik Larry Garfield <larry@garfieldtech.com>
napisał:

> Ilija Tovilo and I are happy to present the first new RFC for PHP 8.3:
> Asymmetric Visibility.
>
> https://wiki.php.net/rfc/asymmetric-visibility
>
> Details are in the RFC, but it's largely a copy of Swift's support for the
> same.
>

Thanks for taking efforts on new RFC.

Why not C# style property accessors? It feels more natural for me while
Swift solution IMHO looks weird/odd composed with PHP class definition
syntax. The arguments in favor from the RFC look forcibly (couldn't find
better word).

Cheers,
Michał Marcin Brzuchalski

---

## #118357 — Larry Garfield — 2022-08-05T19:36:05+00:00

URL: https://externals.io/message/118353#118357 · In reply to: #118354

> Hi,
> Am 05.08.22 um 19:08 schrieb Larry Garfield:
> > […]
>
> Great to hear that there is a new and simple proposals.
>
> One note: In the example about references, I assume the class Baz must 
> extend from Foo.

Yep, thanks, that's a typo on my part.  Fixed now.

--Larry Garfield

---

## #118358 — Larry Garfield — 2022-08-05T19:51:41+00:00

URL: https://externals.io/message/118353#118358 · In reply to: #118356

> Hi Larry,
>
> pt., 5 sie 2022, 19:09 użytkownik Larry Garfield <larry@garfieldtech.com>
> napisał:
>
> > […]
>
> Thanks for taking efforts on new RFC.
>
> Why not C# style property accessors? It feels more natural for me while
> Swift solution IMHO looks weird/odd composed with PHP class definition
> syntax. The arguments in favor from the RFC look forcibly (couldn't find
> better word).
>
> Cheers,
> Michał Marcin Brzuchalski

There's a couple of reasons.  Ilija and I have spent quite a bit of time brainstorming through accessors, and there's still a lot to finalize there, and lots of bikeshed potential.  It's a non-simple problem space.  However, asymmetric visibility is a good stand-alone concept that can work without accessors, and has ample benefits of its own without accessors, and has far less bikeshed potential.  (There's minor debates that might be had over syntax, but the behavior is fairly "it is or it isn't".)

So the primary reason is that this syntax allows us to have asymmetric visibility without having to think about accessors.  We can then, at some point in the future, have a different discussion about accessors and not have to worry about asymmetric visibility in the process.  The C# syntax by nature only works if they come together in the same RFC, or at least one RFC is built such that it locks in some decisions for the other RFC.

As an example, one thing we've been discussing is if accessors should be inline with the property (a la Swift and C#) or annotated methods (as in Javascript and Python).  There's good arguments for both approaches, as well as good arguments against.  But those would have *extremely* different implications for how we represent asymmetric visibility.  So if we took the C# style for asymmetric visibility, that would realistically lock us into C# style accessors even before we've had the discussion about if we want that or not.  That's not good.

(Please don't anyone try to debate which accessor approach to take here; the whole point is we want to introduce asymmetric visibility without having that debate yet.  We'll be able to have it in the future, I'm sure.  There's other knock-on effects as well that I'm skipping for now as not relevant.)

Keeping them separate also makes extending visibility rules in different directions (as noted in the Future Scope section) at some point in the future can also be done without bumping into accessors.  Whether or not those directions are useful is a topic for another time, but it again allows us to discuss those (like a "once" operation or a "package" scope, etc.) without worrying about the impact on accessors.

Secondarily, the C# style means you have to look both before and after the property itself to know what its visibility rules are.  That's increased cognitive load.  This way, all of the various "flags" on a property remain in the same place, and presumably 99.999% of developers will put `public private(set)` right next to each other like that.  That makes the visibility readable all from one spot, and if all you're using is that and not accessors in the future, the impact on your existing reading skills is minimized.  This one is admittedly a bit more subjective, but readability and cognitive load are certainly considerations to, er, consider.

--Larry Garfield

---

## #118359 — Paul Dragoonis — 2022-08-05T20:04:34+00:00

URL: https://externals.io/message/118353#118359 · In reply to: #118358

> 
> > […]
> the
> > […]
>
> There's a couple of reasons.  Ilija and I have spent quite a bit of time
> brainstorming through accessors, and there's still a lot to finalize there,
> and lots of bikeshed potential.  It's a non-simple problem space.  However,
> asymmetric visibility is a good stand-alone concept that can work without
> accessors, and has ample benefits of its own without accessors, and has far
> less bikeshed potential.  (There's minor debates that might be had over
> syntax, but the behavior is fairly "it is or it isn't".)
>
> So the primary reason is that this syntax allows us to have asymmetric
> visibility without having to think about accessors.  We can then, at some
> point in the future, have a different discussion about accessors and not
> have to worry about asymmetric visibility in the process.  The C# syntax by
> nature only works if they come together in the same RFC, or at least one
> RFC is built such that it locks in some decisions for the other RFC.
>
> As an example, one thing we've been discussing is if accessors should be
> inline with the property (a la Swift and C#) or annotated methods (as in
> Javascript and Python).  There's good arguments for both approaches, as
> well as good arguments against.  But those would have *extremely* different
> implications for how we represent asymmetric visibility.  So if we took the
> C# style for asymmetric visibility, that would realistically lock us into
> C# style accessors even before we've had the discussion about if we want
> that or not.  That's not good.
>
> (Please don't anyone try to debate which accessor approach to take here;
> the whole point is we want to introduce asymmetric visibility without
> having that debate yet.  We'll be able to have it in the future, I'm sure.
> There's other knock-on effects as well that I'm skipping for now as not
> relevant.)
>
> Keeping them separate also makes extending visibility rules in different
> directions (as noted in the Future Scope section) at some point in the
> future can also be done without bumping into accessors.  Whether or not
> those directions are useful is a topic for another time, but it again
> allows us to discuss those (like a "once" operation or a "package" scope,
> etc.) without worrying about the impact on accessors.
>
> Secondarily, the C# style means you have to look both before and after the
> property itself to know what its visibility rules are.  That's increased
> cognitive load.  This way, all of the various "flags" on a property remain
> in the same place, and presumably 99.999% of developers will put `public
> private(set)` right next to each other like that.  That makes the
> visibility readable all from one spot, and if all you're using is that and
> not accessors in the future, the impact on your existing reading skills is
> minimized.  This one is admittedly a bit more subjective, but readability
> and cognitive load are certainly considerations to, er, consider.
>



Feedback:

1. The idea is cool

2. when I opened the RFC and looked at the syntax, I wasnt able to
intuitively (self documenting code) figure out what and why was going on.

3. The syntax is heavily implicit as to what each keyword and syntax means,
so the general PHP developer would struggle or mis understand what is going
on.

4. MWOP's point of there being a lot of keywords and prefixes at class
property declaration is not really cluttered (lots going on), is accurate.
Perhaps splitting it into an explicit Attribute might yield a better DX?

Ilina and Larry, all in all I know this is a WIP and you're looking for
early feedback, so this is my feedback, to be considered.

Looking forward to reviewing future revisions of this.

Thanks for the effort and time you've put in so far <3



> --Larry Garfield
>
> --
> 
> To unsubscribe, visit: https://www.php.net/unsub.php

---

## #118360 — Larry Garfield — 2022-08-05T20:25:03+00:00

URL: https://externals.io/message/118353#118360 · In reply to: #118355

> 
>
> > […]
>
> I have two comments:
>
> - For reflection purposes, having two separate methods feels like it will
> be cumbersome; you'd need to check both to determine if you'd need to make
> the reflection property accessible before changing the value. An
> isPublicSet() would alleviate that, or potentially a getSetFlags() method
> against which you could apply a bit mask. You'd need to add a constant for
> public set I both cases.

Reflection is a mess, unfortunately. :-(

There's already separate isPrivate(), isProtected(), and isPublic() methods on ReflectionProperty, so this is consistent.  Also, there's getModifiers() which returns a bitmask; these new flags would be included there.  All these methods all do is check what is syntactically present, both the current ones and proposed ones.

There's definitely room in the Reflection API for a "couldItBeModifiedFromScope($scope)" type method (and similar) that aggregates together symmetric visibility, readonly, asymmetric visibility, etc. into nice booleans.  However, that is notably more work than just adding more syntax flag methods.  It would probably be better as its own RFC, though I'd support that.

If there's consensus that it has to be done here that could be done, but we'd prefer not as it feels like scope creep.

> - The number of items that appear to the left of a property is growing,
> making understanding a declaration increasingly difficult, and your
> discussion of future scope indicates that this is likely to get worse. I'm
> wondering if this sort of behavior could be indicated via attributes
> instead? Something like `#[PropertySetBehavior(PROPERTY_SET_PRIVATE)]`.
> Attributes have the benefit of being separate from the property
> declaration, arguably more readable (one per line), and composable. This
> might play into some of the future scope items as well.

The list indeed groweth, but there's a couple of reasons it wouldn't be appropriate here to use an attribute.

* It would mean get-visibility is a keyword and set-visibility is an attribute.  That's weird and inconsistent.
* There's a strong sense from many on the list that attributes should not be used for language behavior.  Ilija agrees with that position, and I don't care enough to fight it one way or the other.
* An attribute would be ignored in older PHP versions.  For most attributes that's OK, but in this case it would be a potential bug source.  Specifically, it would mean `#[PrivateSet] \n public string $value` would be publicly settable on PHP 8.2, but not publicly settable in 8.3.  That means the integrity of the object is compromised on older versions.  A keyword would make that a syntax error, which in this case is preferable.

> Overall, though, love the design simplicity!

Yay!

--Larry Garfield

---

## #118361 — Robert Landers — 2022-08-06T07:30:05+00:00

URL: https://externals.io/message/118353#118361 · In reply to: #118360

>
> 
> > […]
>
> Reflection is a mess, unfortunately. :-(
>
> There's already separate isPrivate(), isProtected(), and isPublic() methods on ReflectionProperty, so this is consistent.  Also, there's getModifiers() which returns a bitmask; these new flags would be included there.  All these methods all do is check what is syntactically present, both the current ones and proposed ones.
>
> There's definitely room in the Reflection API for a "couldItBeModifiedFromScope($scope)" type method (and similar) that aggregates together symmetric visibility, readonly, asymmetric visibility, etc. into nice booleans.  However, that is notably more work than just adding more syntax flag methods.  It would probably be better as its own RFC, though I'd support that.
>
> If there's consensus that it has to be done here that could be done, but we'd prefer not as it feels like scope creep.
>
> > […]
>
> The list indeed groweth, but there's a couple of reasons it wouldn't be appropriate here to use an attribute.
>
> * It would mean get-visibility is a keyword and set-visibility is an attribute.  That's weird and inconsistent.
> * There's a strong sense from many on the list that attributes should not be used for language behavior.  Ilija agrees with that position, and I don't care enough to fight it one way or the other.
> * An attribute would be ignored in older PHP versions.  For most attributes that's OK, but in this case it would be a potential bug source.  Specifically, it would mean `#[PrivateSet] \n public string $value` would be publicly settable on PHP 8.2, but not publicly settable in 8.3.  That means the integrity of the object is compromised on older versions.  A keyword would make that a syntax error, which in this case is preferable.
>
> > […]
>
> Yay!
>
> --Larry Garfield
>
> --
> 
> To unsubscribe, visit: https://www.php.net/unsub.php
>

Hello,

First of all, this is exciting!

Looking at the behavior for references, can we explore the edges a
bit? (I didn't see any tests that cover this functionality in the PR)

1. Passing a function a reference to a private-set property is
allowed, such as to `sort()`.
2. Returning a reference to a private-set property is allowed and usable.
3. Trying to get a NEW reference to a private-set property outside of
the defined scope is not allowed.

So if I'm understanding correctly, references to private-set
properties can be used outside of the defined scope but they can't be
created outside of the defined scope? If so, should we fire off a
Notice when references are modified outside of the defined scope? I
can see that being annoying in certain contexts, such as sorting an
array, so maybe not? FWIW, this behavior doesn't apply to readonly
properties because attempting to pass a reference to the property
fails.

---

## #118362 — Rowan Tommins — 2022-08-07T10:34:04+00:00

URL: https://externals.io/message/118353#118362 · In reply to: #118359

>2. when I opened the RFC and looked at the syntax, I wasnt able to
>intuitively (self documenting code) figure out what and why was going on.
>
>3. The syntax is heavily implicit as to what each keyword and syntax means,
>so the general PHP developer would struggle or mis understand what is going
>on.


Can you expand on where you think the ambiguity / implicitness is? As I understand it, the RFC is proposing exactly three new combined access levels:

- "public private(set)"
- "public protected(set)"
- "protected private(set)"

Although aesthetically it will take a bit of getting used to, it seems to me pretty clear that the first means "mostly public, but private if you want to set it", and so on.

The only thing I can think of that could be described as "implicit" is that accessing a property by reference is considered a "set" operation, which I'm not sure how any implementation could avoid.

Regards,

-- 
Rowan Tommins
[IMSoP]

---

## #118363 — Lynn — 2022-08-07T10:54:12+00:00

URL: https://externals.io/message/118353#118363 · In reply to: #118362

On Sun, Aug 7, 2022 at 12:34 PM Rowan Tommins <rowan.collins@gmail.com>
wrote:

> Can you expand on where you think the ambiguity / implicitness is? As I
> understand it, the RFC is proposing exactly three new combined access
> levels:
>
> - "public private(set)"
> - "public protected(set)"
> - "protected private(set)"
>
> Although aesthetically it will take a bit of getting used to, it seems to
> me pretty clear that the first means "mostly public, but private if you
> want to set it", and so on.
>
> The only thing I can think of that could be described as "implicit" is
> that accessing a property by reference is considered a "set" operation,
> which I'm not sure how any implementation could avoid.


Personally for me it's the syntax. Reading "public private", "public
protected", or "protected private" reads really weird `public private(set)
static self $property`. In the end it makes sense if you know what it
means, otherwise it's probably confusing. I really like this RFC and I feel
like this might just be the way to go forward, but I have my doubts about
how many more keywords can be realistically added before it becomes a
problem.

---

## #118368 — Claude Pache — 2022-08-07T18:58:25+00:00

URL: https://externals.io/message/118353#118368 · In reply to: #118353

> Le 5 août 2022 à 19:08, Larry Garfield <larry@garfieldtech.com> a écrit :
> 
> Ilija Tovilo and I are happy to present the first new RFC for PHP 8.3: Asymmetric Visibility.
> 
> https://wiki.php.net/rfc/asymmetric-visibility
> 
> Details are in the RFC, but it's largely a copy of Swift's support for the same.
> 
> -- 
>  Larry Garfield
>  larry@garfieldtech.com
> 

Hi,

I like the RFC, and in particular allowing property visibility to remain orthogonal to property accessors.

There are few missing details in the RFC (which is otherwise good):

1. Are static properties supported?

2. What about properties that are overridden/redeclared in a subclass? Given that extending the visibility is generally allowed:

```php
class A { 
	protected int $x;
}
class B extends A {
	public int $x; // ok
}
```

I expect that the following should be allowed as well:

```php
class A { 
	public protected(set) int $x;
}
class B extends A {
	public int $x; // ok
}
```

On the other hand, I expect that the two following cases should *not* be allowed, because it would break encapsulation of class A:

```php
class A { 
	public private(set) int $x;
}
class B extends A {
	public int $x; // error: cannot change a private property to public (or protected)
}
```

and:

```php
class A { 
	public private(set) int $x;
}
class B extends A {
	public private(set) int $x; // error: cannot make a private property in A as private in B
}
```

—Claude

---

## #118369 — drealecs@gmail.com — 2022-08-07T18:59:50+00:00

URL: https://externals.io/message/118353#118369 · In reply to: #118353

On Fri, Aug 5, 2022 at 8:09 PM Larry Garfield <larry@garfieldtech.com>
wrote:

> Ilija Tovilo and I are happy to present the first new RFC for PHP 8.3:
> Asymmetric Visibility.
>
> https://wiki.php.net/rfc/asymmetric-visibility
>
> Details are in the RFC, but it's largely a copy of Swift's support for the
> same.
>
> --
>   Larry Garfield
>   larry@garfieldtech.com
>
> --
> 
> To unsubscribe, visit: https://www.php.net/unsub.php
>
>
Hi Larry, Ilija,

Review the proposal and it looks great.

I have a few subjects to point and maybe discuss further if you think so:

1. The first subject is visible even from the first phrase of the RFC that
uses both the terms "get and set operations" and "read and write
operations".
I'm wondering if you thought about using protected(write) or private(write).

2. The term "asymmetric visibility" is not that accurate and I believe
"asymmetric access" or "asymmetric accessibility" would fit better.
As noted in the relation with __set(), a public private(set) property that
is attempted to be written from global context will not act like it's not
visible but that it's not accessible for writing.

3. I assume the target is both static and non-static properties.
This should be mentioned in the RFC.

Regards,
Alex

---

## #118370 — Larry Garfield — 2022-08-07T19:01:55+00:00

URL: https://externals.io/message/118353#118370 · In reply to: #118363

> On Sun, Aug 7, 2022 at 12:34 PM Rowan Tommins <rowan.collins@gmail.com>
> wrote:
>
> > […]
>
>
> Personally for me it's the syntax. Reading "public private", "public
> protected", or "protected private" reads really weird `public private(set)
> static self $property`. In the end it makes sense if you know what it
> means, otherwise it's probably confusing. I really like this RFC and I feel
> like this might just be the way to go forward, but I have my doubts about
> how many more keywords can be realistically added before it becomes a
> problem.

What syntax would avoid having the visibility keyword repeated?  if you want "public get, private set" behavior, I don't know how that could be done without having the words "public" and "private" both appear somewhere.

Also note, since using private(set) is mutually exclusive with readonly, that the resulting keyword list is about the same length as the existing "public readonly string $foo" that has cropped up frequently with PHP 8.1.  So... yes it's another keyword, but it's not more keywords that can exist simultaneously.  And moving the keyword to the right (a la C#) wouldn't make it any shorter; if anything it would be even more keystrokes.

Very close in size, same in complexity:

public readonly string $foo
public private(set) string $foo

--Larry Garfield

---

## #118371 — Rowan Tommins — 2022-08-07T20:38:45+00:00

URL: https://externals.io/message/118353#118371 · In reply to: #118363

> Reading "public private", "public protected", or "protected private" 
> reads really weird `public private(set) static self $property`.


Interesting, it seems that you've unconsciously broken it up as "public 
private" followed by "(set)", rather than "public" followed by 
"private(set)". Perhaps it's because of the position of the parentheses, 
which do feel awkward to me at first glance. Would it read more 
naturally to you with different punctuation?

public (private set) static self $property;

Or:

(public; private set) static self $property;

Or:

public private-set static self $property;




> I'm wondering if this sort of behavior could be indicated via attributes
> instead? Something like `#[PropertySetBehavior(PROPERTY_SET_PRIVATE)]`.
> Attributes have the benefit of being separate from the property
> declaration, arguably more readable (one per line), and composable.


Attributes are no more intrinsically "separate" or "one per line" than 
keywords; the following would be perfectly valid with the RFC's proposed 
syntax:

class Foo {
    public private(set)
        static int|string $id;
}

And the following would be valid with an attribute like you suggest:

class Foo {
    #[PropertySetBehavior(PROPERTY_SET_PRIVATE)] public static 
int|string $id;
}

Even a shorter attribute name would have the extra punctuation, and the 
restriction that it comes before the normal visibility keyword:

class Foo {
    #[PrivateSet] public static int|string $id;
}


Regards,

-- 
Rowan Tommins
[IMSoP]

---

## #118373 — Ollie Read — 2022-08-07T20:46:19+00:00

URL: https://externals.io/message/118353#118373 · In reply to: #118371

> 
> > […]
> 
> 
> Interesting, it seems that you've unconsciously broken it up as "public 
> private" followed by "(set)", rather than "public" followed by 
> "private(set)". Perhaps it's because of the position of the parentheses, 
> which do feel awkward to me at first glance. Would it read more 
> naturally to you with different punctuation?
> 
> public (private set) static self $property;
> 
> Or:
> 
> (public; private set) static self $property;
> 
> Or:
> 
> public private-set static self $property;
> 
> 
> 
> 
> > […]
> 
> 
> Attributes are no more intrinsically "separate" or "one per line" than 
> keywords; the following would be perfectly valid with the RFC's proposed 
> syntax:
> 
> class Foo {
>    public private(set)
>        static int|string $id;
> }
> 
> And the following would be valid with an attribute like you suggest:
> 
> class Foo {
>    #[PropertySetBehavior(PROPERTY_SET_PRIVATE)] public static 
> int|string $id;
> }
> 
> Even a shorter attribute name would have the extra punctuation, and the 
> restriction that it comes before the normal visibility keyword:
> 
> class Foo {
>    #[PrivateSet] public static int|string $id;
> }
> 
> 
> Regards,
> 
> -- 
> Rowan Tommins
> [IMSoP]
> 
> -- 
> 
> To unsubscribe, visit: https://www.php.net/unsub.php
> 
> 

Just to throw in my ten cents:

I'm not sure if this is because I was recently reading about the fact that PHP doesn't have proper support for a comma operator, but I quite like the idea of providing them as a comma-separated list.

// Public for get and set
public int $number

// Public get, private set
public, private int $number

If a single visibility is provided, same old same old, but if two are provided, they are in the order of get, followed by set. While not being as verbose as providing (set) or other markings to differentiate them, it feels more natural, and it doesn't overcomplicate it.

---
Best Regards,
*Ollie Read*

---

## #118374 — Larry Garfield — 2022-08-07T21:42:29+00:00

URL: https://externals.io/message/118353#118374 · In reply to: #118369

> On Fri, Aug 5, 2022 at 8:09 PM Larry Garfield <larry@garfieldtech.com>
> wrote:
>
> > […]
> Hi Larry, Ilija,
>
> Review the proposal and it looks great.
>
> I have a few subjects to point and maybe discuss further if you think so:
>
> 1. The first subject is visible even from the first phrase of the RFC that
> uses both the terms "get and set operations" and "read and write
> operations".
> I'm wondering if you thought about using protected(write) or private(write).

Not really.  get/set are the terms used in every other language with such a feature, and the words are shorter.  There's really no compelling reason to use a longer, less common keyword.

> 3. I assume the target is both static and non-static properties.
> This should be mentioned in the RFC.

That's the intent, although, now that you mention it, that part hasn't been implemented yet. :-)  Ilija will add that to the patch and we'll update the RFC when it's been added.  Thanks for the catch.

--Larry Garfield

---

## #118375 — Larry Garfield — 2022-08-07T21:48:11+00:00

URL: https://externals.io/message/118353#118375 · In reply to: #118373

> Just to throw in my ten cents:
>
> I'm not sure if this is because I was recently reading about the fact 
> that PHP doesn't have proper support for a comma operator, but I quite 
> like the idea of providing them as a comma-separated list.
>
> // Public for get and set
> public int $number
>
> // Public get, private set
> public, private int $number
>
> If a single visibility is provided, same old same old, but if two are 
> provided, they are in the order of get, followed by set. While not 
> being as verbose as providing (set) or other markings to differentiate 
> them, it feels more natural, and it doesn't overcomplicate it.
>
> ---
> Best Regards,
> *Ollie Read*

I'm not wild about implicit ordering as a syntax, for two main reasons:

1) It requires implicit knowledge on the part of the reader what's going on.  Any kind of explicit syntax makes it clear "public, except for this operation".  ("this operation" for now being just set, but then the set is right there and self-documenting.)

2) It makes extending it to other operations like "once" or "init" in the future impossible.  (See Future Scope.)  If we went with public, private string $foo, which implicitly meant "public(get), private(set)", and then in the future we add a "once" operation to mimic the behavior of readonly... what would that look like?  With (set), it's self-evident how to extend the syntax.  With it being fully implicit, I cannot think of a way to do so.

Something like public private:set (colon instead of parens) would work just as well, if the () are confusing somehow, but that doesn't feel like a common problem.  And it would lose the parallelism with Swift.

--Larry Garfield

---

## #118376 — Juliette Reinders Folmer — 2022-08-08T01:10:37+00:00

URL: https://externals.io/message/118353#118376 · In reply to: #118353

> Ilija Tovilo and I are happy to present the first new RFC for PHP 8.3: Asymmetric Visibility.
>
> https://wiki.php.net/rfc/asymmetric-visibility
>
> Details are in the RFC, but it's largely a copy of Swift's support for the same.
>

For what it's worth, here's my initial thoughts after reading the RFC:

1. In "Permitted visibility, it explicitly states that the "set" scope 
MUST be more restrictive than the "get" scope. However, no reason for 
this is provided. Please make the argumentation to support this choice 
explicit.

2. In "Interaction with __set" and "Relationship with readonly" the RFC 
states it wants to be consistent with the readonly behaviour, but that 
is a different keyword. IMHO, these modifiers should work independently 
of each other and are now improperly closely binded. If one wants 
readonly behaviour, use the `readonly` keyword.

3. I only see a hat-tip to the property accessors RFC regarding the 
chosen syntax.
I'd like to see a more extensive discussion and consideration of various 
possible syntaxes.
Some potential alternatives:
- "visibility(get) visibility(set) type $prop" - which makes it explicit 
what each visibility applies to
- "visibility(set: visibility) type $prop" - where it is clear that the 
"main" visibility is the default and anything within the brackets a 
deviation. This syntax would also more easily allow for expanding, like 
"visibility(set: visibility, unset: visibility) type $prop"

I understand the desire for parallelism with Swift, but I don't think 
that's a valid argument. The syntax should be right for PHP and if that 
would happen to overlap/sync with a syntax used elsewhere: great, but 
that's not an argument to only look at that syntax without a proper 
discussion and due consideration for alternatives.

4. While I see no mention of "multi-property declarations", I presume 
the visibility would apply to all ? I think it would be good to mention 
this explicitly in the RFC as well.
     * By "multi-property declarations", I mean: `public string $type, 
$value = 'me', $somethingElse;

5. Are there tokenizer implications ? How will `set` (and possibly 
`get`) be tokenized ?

6. As this is something which can already be handled via magic methods, 
is there sufficient reason to make the language/engine more complicated 
to support this via a syntax ?

Overall, I mainly just see an idea in the RFC, but no justification for 
the need for it, let alone for any of the choices made.

Smile,
Juliette

---

## #118377 — Mike Schinkel — 2022-08-08T02:54:02+00:00

URL: https://externals.io/message/118353#118377 · In reply to: #118353

> 
> 
> Ilija Tovilo and I are happy to present the first new RFC for PHP 8.3: Asymmetric Visibility.
> 
> https://wiki.php.net/rfc/asymmetric-visibility



Conceptually, I believe this would add useful capabilities to PHP that are impossible to implement without:

a.) Likely-sizable performance penalties, and 
b.) Unfortunate edge-cases; e.g. `property_exists()`, `get_object_vars()`, etc.

Still, here are my concerns with the RFC as it currently exists:

1.) The syntax of "<keyword>(<keyword>)" feels inconsistent with the rest of the language. 

PHP has function(args...) and (<type>), both of which can be viewed as "a function of 'args'." However, "private(set)" is basically a constraint, not a "function of 'set'."

PHP also has function(args...):<type> which is a constraint so I propose we consider the following syntax instead:

  <visibility>:<operation>

Thus all of these:

 public:get
 public:set
 private:get
 private:set
 protected:get
 protected:set

2.)The idiom "public private:set" is hard to reason about when reading, at least for me. 

It also feels inconsistent in that it requires one type of visibility to be constrained, but not the other. 

Speaking of "explicit" from one of Larry's latest replies, I propose we consider requiring asymmetric visibility *explicitly* declared, in all cases. Requiring explicit declaration would: 

a.) Make asymmetric visibility easier to reason about, and 
b.) Ensure consistency when asymmetric visibility is used.

Specifically I would propose we make these INVALID:

public protected:set <type> $<var>;
public private:set <type> $<var>;
protected private:set <type> $<var>;


But all of these following VALID:

These as-is for backward compatibility and an obvious default behavior:

public [<type>] $<var>;
protected [<type>] $<var>;
private [<type>] $<var>;

And these where behavior is explicit:

public:get protected:set <type> $<var>;
public:get private:set <type> $<var>;
protected:get private:set <type> $<var>;

As well as these:

public:set protected:get <type> $<var>;
public:set private:get <type> $<var>;
protected:set private:get <type> $<var>;

And of course these:

public:get public:set <type> $<var>;
private:get private:set <type> $<var>;
protected:get protected:set <type> $<var>;


3.) I have concerns about the proposed methods isProtectedSet() and isPrivateSet().  

These names feels like we are asking if some thing "Set" is "Protected" or "Private" where no such "thing" exists in this context.  

In other words it reads as if you are asking "Is it a *protected* Set?" or "Is it a *private* Set?"  Such naming also does not relate to the Property itself which is what the prefix verb "is" is acting on.

I would propose instead we consider the following are these instead would be asking the *ability* to "Set" the Property is "Protected" or "Private" where The Property is again what the prefix verb "is" is acting on:

isSetProtected()
isSetPrivate()
isGetProtected(), and
isGetPrivate()


#jmtcw

-Mike

---

## #118378 — Andreas Heigl — 2022-08-08T06:03:51+00:00

URL: https://externals.io/message/118353#118378 · In reply to: #118370

Hey Larry, hey all


> 
> > […]
> 
> What syntax would avoid having the visibility keyword repeated?  if you want "public get, private set" behavior, I don't know how that could be done without having the words "public" and "private" both appear somewhere.
> 
> Also note, since using private(set) is mutually exclusive with readonly, that the resulting keyword list is about the same length as the existing "public readonly string $foo" that has cropped up frequently with PHP 8.1.  So... yes it's another keyword, but it's not more keywords that can exist simultaneously.  And moving the keyword to the right (a la C#) wouldn't make it any shorter; if anything it would be even more keystrokes.
> 
> Very close in size, same in complexity:
> 
> public readonly string $foo
> public private(set) string $foo

In my opinion those are actually two different things.

One is allowing a variable to be publicly readably and writeable. But it 
can only be set once. Every subsequent call to setting a new value will 
fail.

The other is allowing different scopes for reading and writing the 
variable. The variable can be read publicly but can only be set from 
within the current class-context. But setting it from the current class 
context can happen multiple times.

So it should indeed be possible to combine both possibilities:

public(get) private(set) readonly string $foo;

My 0.02€

Cheers

Andreas

-- 
                                                               ,,,
                                                              (o o)
+---------------------------------------------------------ooO-(_)-Ooo-+
| Andreas Heigl                                                       |
| mailto:andreas@heigl.org                  N 50°22'59.5" E 08°23'58" |
| https://andreas.heigl.org                                           |
+---------------------------------------------------------------------+
| https://hei.gl/appointmentwithandreas                               |
+---------------------------------------------------------------------+

---

## #118379 — Casper Langemeijer — 2022-08-08T07:54:03+00:00

URL: https://externals.io/message/118353#118379 · In reply to: #118378

Hi all,

In the discussion I sometimes see the terminology 'readonly' and 'writable' being used. This is confusing because when the property is an object that itself is mutable, there is nothing read-only about it.

The terminology in the RFC seems right to me, and overall it seems solid.

However, I'm not convinced this RFC is solving a real issue. I could not find any reasoning in the RFC, except that Swift has a very similar language feature.

Grtz, Casper

---

## #118380 — Andreas Heigl — 2022-08-08T08:03:16+00:00

URL: https://externals.io/message/118353#118380 · In reply to: #118379

Hey Casper.


> Hi all,
> 
> In the discussion I sometimes see the terminology 'readonly' and 'writable' being used. This is confusing because when the property is an object that itself is mutable, there is nothing read-only about it.
> 
> The terminology in the RFC seems right to me, and overall it seems solid.
> 
> However, I'm not convinced this RFC is solving a real issue. I could not find any reasoning in the RFC, except that Swift has a very similar language feature.
> 

To me it solves the topic of making a property readable but not 
writeable from the public while still allowing it to be written to 
within the private or protected context.

So enforcing usage of a public setter-metbhod but not having to use a 
getter.

Soemthing like this

final class Foo
{
     public private(set) string $username;

     public function changeUsernameTo(string $newUsername): self
     {
         if (! newUsernameIsUnique($newUsername) {
             throw new RuntimeException('The username is not unique');
         }
         $this->username = $newUsername;

         return $this;
     }
}


readonly only solves that for immutable properties but there currently 
is no way of solving that for mutable properties.

Just my 0.02€

Cheers

Andreas

-- 
                                                               ,,,
                                                              (o o)
+---------------------------------------------------------ooO-(_)-Ooo-+
| Andreas Heigl                                                       |
| mailto:andreas@heigl.org                  N 50°22'59.5" E 08°23'58" |
| https://andreas.heigl.org                                           |
+---------------------------------------------------------------------+
| https://hei.gl/appointmentwithandreas                               |
+---------------------------------------------------------------------+

---

## #118381 — michal.brzuchalski@gmail.com — 2022-08-08T08:09:10+00:00

URL: https://externals.io/message/118353#118381 · In reply to: #118370

Hi Larry,

niedz., 7 sie 2022 o 21:02 Larry Garfield <larry@garfieldtech.com>
napisał(a):

> 
> > […]
> to
> > […]
> private(set)
> > […]
> feel
> > […]
>
> What syntax would avoid having the visibility keyword repeated?  if you
> want "public get, private set" behavior, I don't know how that could be
> done without having the words "public" and "private" both appear somewhere.
>
> Also note, since using private(set) is mutually exclusive with readonly,
> that the resulting keyword list is about the same length as the existing
> "public readonly string $foo" that has cropped up frequently with PHP 8.1.
> So... yes it's another keyword, but it's not more keywords that can exist
> simultaneously.  And moving the keyword to the right (a la C#) wouldn't
> make it any shorter; if anything it would be even more keystrokes.
>
> Very close in size, same in complexity:
>
> public readonly string $foo
> public private(set) string $foo
>

Since the radio request show already started wanted to push my 0.50€

Instead of thinking how to improve visibility on the left side, let's move
all the visibility modifiers to the right
but enclosed with parentheses, this way a future stays open for
improvements and introduction of accessors.

// instead of
class Foo {
    public private(set) static int|string $id;
}

// let's do this
class Bar {
    static int|string $id {
        public;
        private set;
    };
}

A future improvement may add an accessor body on the right side of "set".

Cheers :D,
Michał Marcin Brzuchalski

---

## #118382 — Marco Pivetta — 2022-08-08T08:14:50+00:00

URL: https://externals.io/message/118353#118382 · In reply to: #118380

Heyo Andreas, Casper,



> Hey Casper.
>
> 
> > […]
> 'writable' being used. This is confusing because when the property is an
> object that itself is mutable, there is nothing read-only about it.
> > […]
> find any reasoning in the RFC, except that Swift has a very similar
> language feature.
> > […]
>
> To me it solves the topic of making a property readable but not
> writeable from the public while still allowing it to be written to
> within the private or protected context.
>
> So enforcing usage of a public setter-metbhod but not having to use a
> getter.
>
> Soemthing like this
>
> final class Foo
> {
>      public private(set) string $username;
>
>      public function changeUsernameTo(string $newUsername): self
>      {
>          if (! newUsernameIsUnique($newUsername) {
>              throw new RuntimeException('The username is not unique');
>          }
>          $this->username = $newUsername;
>
>          return $this;
>      }
> }
>
>
> readonly only solves that for immutable properties but there currently
> is no way of solving that for mutable properties.
>

Similar question as Casper here: I use `readonly` properties aggressively,
and I try to make the state as immutable as possible.

In the **extremely** rare cases where `public get` and `private set` are
needed, I rely on traditional getters and setters, which are becoming
extremely situational anyway, and still work perfectly fine.
If that doesn't work, then PHPStan/Psalm allow declaring `@private`,
`@internal`, `@phpstan-internal` or similar mechanisms to restrict scope
access. `qossmic/deptrac` also works wonders here, compared to PHP.

In fact, I'm writing so few getters and setters these days, that I don't
see why I'd need getter and setter semantics to creep into the language,
especially mutable ones, not even with the reflection improvements.

As for `readonly`, the reason we sometimes **cannot** use `readonly` is
because current `clone` semantics can't work around `readonly` rules
(discussed in the `readonly` RFC): https://3v4l.org/og8bn
If we solved that, I think `private(set)` would become even more
situational, if not completely unnecessary.

Another puzzling area in this RFC is reference support: let's take the
occasion to disallow references completely on asymmetric access, perhaps?
It would be nice to see references gone for good. No need to pollute the
engine for the edge case of `ArrayAccess` and similar APIs :P

Marco Pivetta

https://twitter.com/Ocramius

https://ocramius.github.io/

---

Next page: https://externals.io/message/118353.md?page=2
