ID: 30588
Updated by: [EMAIL PROTECTED]
Reported By: auroraeosrose at hotmail dot com
Status: Open
-Bug Type: *General Issues
+Bug Type: Documentation problem
-Operating System: WinXP SP2
+Operating System: *
-PHP Version: 5CVS-2004-10-27 (dev)
+PHP Version: 5.*
New Comment:
Foreach uses the current visibility. That is private and protected
inside the class and from outside the class only the public member
variables.
Previous Comments:
------------------------------------------------------------------------
[2004-10-27 20:11:49] auroraeosrose at hotmail dot com
Description:
------------
I have no idea whether this should be classified as an engine problem,
or a documentation problem.
When using BASIC foreach over an object inside a class method, (no
fancy spl stuff) it does not behave as documented on php.net - showing
only public properties. Foreach will have access to all properties
that the function/method it's used in has access to. This means
private and protected variables can be foreached inside the class. Now
this might be useful in some cases, but for me it's a headache because
there is no way to tell whether a property is protected/private/public
without using reflection - which is way too much overhead when you just
want to unset all the public properties in a class. Right now I have to
use a seperate function to do the unsetting, but it's a pain.
In conclusion: either foreach needs to be changed or the docs need to
be changed. If foreach CAN access all properties a function/method has
access to, there should be a QUICK, EASY way to find out if a property
is public/private/protect (e.g. is_public) so reflection doesn't have
to be hauled out just to find out that information.- I could play with
spl's arrayobject a bit but it still doesn't address the
documentation/behavior mismatch.
P.S. get_object_vars is affect by this as well
Reproduce code:
---------------
<?php
class test {
public $fruit = 'orange';
protected $veggie = 'carrot';
private $meat = 'beef';
public function loop()
{
foreach($this as $key => $item)
{
echo 'property '.$key.' = '.$item."\n";
}
}
}
$obj = new test();
$obj->loop();
foreach($obj as $key => $item)
{
echo 'property '.$key.' = '.$item."\n";
}
?>
Expected result:
----------------
property fruit = orange
property fruit = orange
Actual result:
--------------
property fruit = orange
property veggie = carrot
property meat = beef
property fruit = orange
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=30588&edit=1