[php-src] Issue #15912: Assigning property value through readonly array results in "Cannot modify readonly property"
| From: | SunMar | Date: | Mon, 16 Sep 2024 09:16:27 +0000 |
| Subject: | [php-src] Issue #15912: Assigning property value through readonly array results in "Cannot modify readonly property" | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-247797@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/15912
Author: SunMar
### Description
The following code:
```php
<?php
class A {
public function __construct(
public readonly array $a,
) {}
}
class B {
public function __construct(
public string $b,
) {
}
}
$a = new A([ new B('foo') ]);
$a->a[0]->b = 'bar';
echo $a->a[0]->b;
```
Resulted in this output:
```
PHP Fatal error: Uncaught Error: Cannot modify readonly property A::$a in /test.php:17
```
But I expected this output instead:
```
bar
```
If you try to change the property of an object that is inside a readonly array, based on syntax it
thinks you're change the array itself and throws a Fatal, even though that property we're
actually trying to change (
$a->a[0]->b) is not readonly and can be changed. If
the code to change the B::$b property is refactored like this the code works as
expected:
```php
$a = new A([ new B('foo') ]);
$b = $a->a[0];
$b->b = 'bar';
```
### PHP Version
8.2.23, 8.3.11
### Operating System
_No response_