|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2004-10-27 21:55 UTC] helly@php.net
[2004-10-28 04:55 UTC] curt@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Tue Mar 10 09:00:01 2026 UTC |
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