Re: [RFC] Asymmetric Visibility, with readonly
| From: | Larry Garfield | Date: | Wed, 30 Nov 2022 19:40:00 +0000 |
| Subject: | Re: [RFC] Asymmetric Visibility, with readonly | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-119066@lists.php.net to get a copy of this message | ||
On Wed, Nov 30, 2022, at 12:10 PM, Claude Pache wrote:
> Hi,
>
> What is the behaviour of the following code?
>
> ```php
> class Foo {
> public private(set) array $bar = [ ];
> }
>
> $foo = new Foo;
>
> $foo->bar['x'] = 'y'; // error?
>
> var_dump(isset($foo->bar['x'])); // true?, false?
> ```
>
> I think that modification of an array should require write access?
> (That should be clarified in the RFC.)
Correct, that would not be allowed. Array writes are always indirect modification, because
it's two steps to get to it. That will error out in this case, as one would expect from
private(set).
--Larry Garfield