php.net |  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: None
 [2005-05-28 14:39 UTC] 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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-06-06 09:52 UTC] dmitry@php.net
Fixed in CVS HEAD and PHP_5_0.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 03:01:29 2024 UTC