|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #33171 foreach enumerates private fields declared in base classes
Submitted: 2005-05-28 14:39 UTC Modified: 2005-06-06 09:52 UTC
From: ladislav dot prosek at matfyz dot cz Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 5.0.4 OS: Win XP Pro SP2
Private report: No CVE-ID:
 [2005-05-28 14:39 UTC] ladislav dot prosek at matfyz dot cz
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;

Expected result:
c => B's c

Actual result:
c => B's c
c => A's c


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2005-06-06 09:52 UTC]
Fixed in CVS HEAD and PHP_5_0.
PHP Copyright © 2001-2015 The PHP Group
All rights reserved.
Last updated: Mon Oct 05 04:01:32 2015 UTC