#46416 [Bgs]: each() erratic behaviour with objects
| From: | bugzilla at zuavra dot net | Date: | Wed, 29 Oct 2008 12:10:36 +0000 |
| Subject: | #46416 [Bgs]: each() erratic behaviour with objects | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-130275@lists.php.net to get a copy of this message | ||
ID: 46416
User updated by: bugzilla at zuavra dot net
Reported By: bugzilla at zuavra dot net
Status: Bogus
Bug Type: Scripting Engine problem
Operating System: *
PHP Version: 5.3.0alpha2
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2008-10-29 11:30:54] 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 10:06:26] 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
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=46416&edit=1