Re: Re[2]: [PHP] nested, referenced foreach & implicit current array pointer issues



2007/1/30, speedy <speedy.spam@xxxxxxxxx>:

Hello Martin,

> Tuesday, January 30, 2007, 8:45:50 PM, you wrote:
>
> function f()
> {
> global $arr;
>
> foreach($arr as $k=>$v) {
> $v->do_something();
> }
> }
>
> I don't see your point anywhere... foreach iterates over a copy of
> the array so where is the potential side-effect? Even so there will
> be references if the items of the original array are references, or
> if they are objects in PHP5, but that's expected behaviour. That's
> the way many OOP languages have been implemented (eg: Java) without
> coders having the problems you mention. Just give me one good
> reason to even consider your proposal.


Humm, you seem to have misunderstood (& that could be one reason in
itself). In my example foreach() iterates over the original $arr array
(contrary to what you say) - and so may deeply nested fns in the called
do_something() method, making global + foreach construct
unuseable/"unstable" in non-trivial cases.


Damn, I hate localized documentation. The spanish manual isn't updated to
what foreach does in the latest PHP5, and I didn't bother to check the
english version. Foreach uses a copy only if the array is not a reference,
and that's why you have that problem. There is nothing wrong with the global
keyword, just use $GLOBAL['arr'] instead to avoid the reference, or create a
copy by assignment.

Anyway, I couldn't find this change of behaviour in the foreach in the
changelog, but it seems it have been introduced around may 2005. Maybe is
too late to complain, but, now that I know, I think this behaviour doesn't
seem right.


Relevant Pages

  • Re: [PHP] foreach() using current() strange beahvior
    ... that's expected as foreach moves the internal array pointer, ... When using the internal pointer just by calling current(so not moving ... Unless the array is referenced, foreach operates on a copy of the ...
    (php.general)
  • Re: a question about for and foreach
    ... foreach (@array) { ... why @array are changed after this foreach loop!!! ... The "foreach" loop iterates over a normal list value and sets the ... If any element of LIST is an lvalue, you can modify it by modifying ...
    (perl.beginners)
  • Re: safe to delete elements of array in foreach
    ... I agree with Jon on this one. ... I make it a habit not to delete entries in a foreach() loop. ... build an array of keys I want to delete, and after the loop ends, delete ... I don't know whether an operation like this is guaranteed to work in PHP ...
    (comp.lang.php)
  • Re: [PHP] Foreach
    ... The problem I am having is getting both to update the table in MySQL. ... echo "$t"; ... foreach { ... // foreach expects an array, ...
    (php.general)
  • Re: safe to delete elements of array in foreach
    ... (normally this is not the case but not sure in php) ... I make it a habit not to delete entries in a foreach() loop. ... build an array of keys I want to delete, and after the loop ends, ...
    (comp.lang.php)