Skip to content

Foreach by reference ignores property visibility #24280

Open

Description

@kamil-tekiela

Description

The following code:

<?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

https://3v4l.org/oL4rA#v

Operating System

No response

Activity

  1. added 2 commits that reference this issue on Oct 17, 2026
  2. added 4 commits that reference this issue on Oct 17, 2026
    7b5662a
    91324c0
    a5c96de
    cf991cb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions