#33171 [Ver->Csd]: foreach enumerates private fields declared in base classes
| From: | dmitry@php.net | Date: | Mon, 06 Jun 2005 07:52:42 +0000 |
| Subject: | #33171 [Ver->Csd]: foreach enumerates private fields declared in base classes | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-79927@lists.php.net to get a copy of this message | ||
ID: 33171
Updated by: dmitry@php.net
Reported By: ladislav dot prosek at matfyz dot cz
-Status: Verified
+Status: Closed
Bug Type: Zend Engine 2 problem
Operating System: Win XP Pro SP2
PHP Version: 5.0.4
New Comment:
Fixed in CVS HEAD and PHP_5_0.
Previous Comments:
------------------------------------------------------------------------
[2005-05-28 14:39:46] ladislav dot prosek at matfyz dot cz
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 this bug report at http://bugs.php.net/?id=33171&edit=1