php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46416 each() erratic behaviour with objects
Submitted: 2008-10-29 10:06 UTC Modified: 2008-10-29 12:10 UTC
From: bugzilla at zuavra dot net Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.0alpha2 OS: *
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: bugzilla at zuavra dot net
New email:
PHP Version: OS:

 

 [2008-10-29 10:06 UTC] bugzilla at zuavra dot net
Description:
------------
Using each() to iterate through an object will produce weird results in two respects:
1. It will expose private and protected members alongside public ones.
2. For private and protected members, the keys (names) are jumbled.

Yes, I know that this behaviour is known and that the manual page for each() warns about this. But such an inconsistency should not exist.

Desirable consistent behaviour:
a) Produce the same results as foreach() (show only public members).
or
b) Refuse to work with objects altogether and state so in the manual.

Reproduce code:
---------------
<?php
class Example {
    private $priv = 1;
    protected $prot = 2;
    public $pub = 3;
}
$obj = new Example();
while (list($key, $val) = each($obj)) {
    printf("%s = %s\n", $key, $val);
}
?>

Expected result:
----------------
pub = 3

Actual result:
--------------
[NUL]Example[NUL]priv = 1
[NUL]*[NUL]prot = 2
pub = 3

where [NUL] is the null (0x00) character

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-10-29 11:30 UTC] jani@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php


 [2008-10-29 12:10 UTC] bugzilla at zuavra dot net
Yeah, I expected I might get the canned response for this one.

I still think it doesn't make sense for each() to behave in a completely unpredictable manner.

I suspect the rationalization for this is that each() accesses objects in "string context" as opposed to "object/class context" and therefore it need not observe access modifiers. However, it doesn't explain why foreach should behave otherwise.

I also saw rationalization for print_r, var_dump etc. in that those are special and that they should be allowed unobstructed view of objects. I don't see how that applies to each().

At the very least please consider making each() return proper names for private/protected variables.

If there are internal engine considerations that require that each() be kept (partially or totally) with these inconsistencies, for now or for longer, please say so in this or another bug report, otherwise I suspect this issue may very well keep on being raised.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Dec 01 02:00:01 2025 UTC