#33171 [NEW]: foreach enumerates private fields declared in base classes

From: Date: Sat, 28 May 2005 12:39:47 +0000
Subject: #33171 [NEW]: foreach enumerates private fields declared in base classes
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-79646@lists.php.net to get a copy of this message
From: ladislav dot prosek at matfyz dot cz Operating system: Win XP Pro SP2 PHP version: 5.0.4 PHP Bug Type: Zend Engine 2 problem Bug description: foreach enumerates private fields declared in base classes Description: ------------ Using foreach on $this results in enumerating not only visible fields (i.e. public and protected field declared along the inheritance hierarchy + private fields declared in current class) but also all private fields declared along the inheritance hierarchy. This is wrong since those fields are not visible and should not be accessible. By the way, the foreach documentation page still says "foreach works only on arrays...". Perhaps you should consider updating it ;-) Reproduce code: --------------- class A { private $c = "A's c"; } class B extends A { private $c = "B's c"; public function go() { foreach ($this as $key => $val) { echo "$key => $val\n"; } } }; $x = new B; $x->go(); Expected result: ---------------- c => B's c Actual result: -------------- c => B's c c => A's c -- Edit bug report at http://bugs.php.net/?id=33171&edit=1 -- Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=33171&r=trysnapshot4 Try a CVS snapshot (php5.0): http://bugs.php.net/fix.php?id=33171&r=trysnapshot50 Try a CVS snapshot (php5.1): http://bugs.php.net/fix.php?id=33171&r=trysnapshot51 Fixed in CVS: http://bugs.php.net/fix.php?id=33171&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=33171&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=33171&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=33171&r=needscript Try newer version: http://bugs.php.net/fix.php?id=33171&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=33171&r=support Expected behavior: http://bugs.php.net/fix.php?id=33171&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=33171&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=33171&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=33171&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=33171&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=33171&r=dst IIS Stability: http://bugs.php.net/fix.php?id=33171&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=33171&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=33171&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=33171&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=33171&r=mysqlcfg

« previous php.bugs (#79646) next »