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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ladislav dot prosek at matfyz dot cz
New email:
PHP Version: OS:

 

 [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: Thu May 09 08:01:34 2024 UTC