#30588 [Opn->Csd]: foreach on $this shows private/protected
| From: | curt@php.net | Date: | Thu, 28 Oct 2004 02:55:05 +0000 |
| Subject: | #30588 [Opn->Csd]: foreach on $this shows private/protected | ||
| References: | 1 | Groups: | php.doc |
| Request: | Send a blank email to phpdoc+get-969364683@lists.php.net to get a copy of this message | ||
ID: 30588
Updated by: curt@php.net
Reported By: auroraeosrose at hotmail dot com
-Status: Open
+Status: Closed
Bug Type: Documentation problem
Operating System: *
PHP Version: 5.*
New Comment:
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.
Thank you for the report, and for helping us make our documentation
better.
Adjusted documentation to reflect this behaviour.
Previous Comments:
------------------------------------------------------------------------
[2004-10-27 21:55:41] helly@php.net
Foreach uses the current visibility. That is private and protected
inside the class and from outside the class only the public member
variables.
------------------------------------------------------------------------
[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