[php-src] Issue #24280: Foreach by reference ignores property visibility
| From: | kamil-tekiela | Date: | Mon, 12 Oct 2026 15:19:13 +0000 |
| Subject: | [php-src] Issue #24280: Foreach by reference ignores property visibility | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-252979@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/24280
Author: kamil-tekiela
### Description
The following code:
```php
<?php
class U { public private(set) string $name = "orig"; }
$u = new U;
foreach ($u as &$v) { $v = "X"; } // no error, $u->name is now "X"
var_dump($u->name);
$r = &$u->name; // Error: Cannot indirectly modify private(set) property
```
Resulted in this output:
```
string(1) "X"
Fatal error: Uncaught Error: Cannot indirectly modify private(set) property U::$name from global
scope
```
But I expected this output instead:
```
Fatal error: Uncaught Error: Cannot indirectly modify private(set) property U::$name from global
scope
```
### PHP Version
```plain
https://3v4l.org/oL4rA#v
```
### Operating System
_No response_