php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #30588 foreach on $this shows private/protected
Submitted: 2004-10-27 20:11 UTC Modified: 2004-10-28 04:55 UTC
From: auroraeosrose at hotmail dot com Assigned:
Status: Closed Package: Documentation problem
PHP Version: 5.* 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: auroraeosrose at hotmail dot com
New email:
PHP Version: OS:

 

 [2004-10-27 20:11 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-10-27 21:55 UTC] 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-28 04:55 UTC] curt@php.net
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.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Tue Mar 10 10:00:02 2026 UTC