#30588 [NEW]: foreach on $this shows private/protected
| From: | auroraeosrose at hotmail dot com | Date: | Wed, 27 Oct 2004 18:11:56 +0000 |
| Subject: | #30588 [NEW]: foreach on $this shows private/protected | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-68404@lists.php.net to get a copy of this message | ||
From: auroraeosrose at hotmail dot com
Operating system: WinXP SP2
PHP version: 5CVS-2004-10-27 (dev)
PHP Bug Type: *General Issues
Bug description: foreach on $this shows private/protected
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 bug report at http://bugs.php.net/?id=30588&edit=1
--
Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=30588&r=trysnapshot4
Try a CVS snapshot (php5.0): http://bugs.php.net/fix.php?id=30588&r=trysnapshot50
Try a CVS snapshot (php5.1): http://bugs.php.net/fix.php?id=30588&r=trysnapshot51
Fixed in CVS: http://bugs.php.net/fix.php?id=30588&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=30588&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=30588&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=30588&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=30588&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=30588&r=support
Expected behavior: http://bugs.php.net/fix.php?id=30588&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=30588&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=30588&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=30588&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=30588&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=30588&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=30588&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=30588&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=30588&r=float
MySQL Configuration Error: http://bugs.php.net/fix.php?id=30588&r=mysqlcfg