Category
bug · php-src-strict · language / object iteration
Problem
Zend foreach ($obj as $k => $v) only yields properties visible from the current scope (same visibility rules as get_object_vars($obj)). The VM iterates every declared instance property regardless of public/protected/private, and also leaks parent private props into a child-class foreach ($this).
| Repro |
Zend 8.2 (2026-07-26) |
VM (2026-07-26) |
external foreach on class with public/protected/private |
pub=P only |
pub=P,pro=R,pri=V |
foreach ($this) inside declaring class |
all three props |
all three (ok) |
foreach ($this) in subclass |
public + protected (+ subclass privates); not parent private |
includes parent pri=V |
| anonymous class external foreach (public+private) |
public only |
both |
php-src reference
PHP implementation target
- Object-property foreach path in
lib/VM/ (and JIT foreach-object helper if shared) — filter by declaring scope / visibility before yielding; match get_object_vars scope rules
- No new C runtime
Repro
./script/docker-exec.sh -- bash -lc 'cat >/tmp/repro_foreach_vis.php <<'"'"'PHP'"'"'
<?php
class A {
public $pub = "P";
protected $pro = "R";
private $pri = "V";
public function inside() {
$ks = [];
foreach ($this as $k => $v) { $ks[] = "$k=$v"; }
return implode(",", $ks);
}
}
class B extends A {
public function child() {
$ks = [];
foreach ($this as $k => $v) { $ks[] = "$k=$v"; }
return implode(",", $ks);
}
}
$ks = [];
foreach (new A() as $k => $v) { $ks[] = "$k=$v"; }
echo "ext:", implode(",", $ks), "\n";
echo "in:", (new A())->inside(), "\n";
echo "ch:", (new B())->child(), "\n";
PHP
php /tmp/repro_foreach_vis.php
php bin/vm.php /tmp/repro_foreach_vis.php'
Done when
Category
bug· php-src-strict · language / object iterationProblem
Zend
foreach ($obj as $k => $v)only yields properties visible from the current scope (same visibility rules asget_object_vars($obj)). The VM iterates every declared instance property regardless ofpublic/protected/private, and also leaks parentprivateprops into a child-classforeach ($this).foreachon class with public/protected/privatepub=Ponlypub=P,pro=R,pri=Vforeach ($this)inside declaring classforeach ($this)in subclasspri=Vphp-src reference
Zend/zend_execute.c— object property foreach /zend_check_property_accessZend/zend_object_handlers.c—zend_std_get_properties_for/ visibility filteringPHP implementation target
lib/VM/(and JIT foreach-object helper if shared) — filter by declaring scope / visibility before yielding; matchget_object_varsscope rulesRepro
Done when
.phptundertest/compliance/cases/language/