nested, referenced foreach & implicit current array pointer issues
- From: speedy.spam@xxxxxxxxx (speedy)
- Date: Tue, 30 Jan 2007 19:31:49 +0100
Hello PHP crew,
As a followup to:
http://bugs.php.net/bug.php?id=22879
I've stumbled upon this problem in a way:
function f()
{
global $arr;
foreach($arr as $k=>$v) {
$v->do_something();
}
}
After digging through the docs I found that $arr is in fact a reference to original $arr, and (in a pretty complex/confusing doc. page about foreach) came to a conclusion that the above construct is practically unusable with the global keyword as do_something() can potentially affect $arr hidden & implicit (*yuck*) current element pointer and thus quite _non-obviously_ side-effect parent foreach() (ie. side-effect hidden in the depths of its fn calls).
It would be (perhaps) better if PHP would treat global as making globaly defined $arr accessible from the local fn. namespace instead of assigning a local name to be a reference?
Also if the price of implicit current element pointer is copying of non-referenced array on foreach() to gain humanly-expected behaviour,
and its great potential of writing non-obvious/bad code, I must wonder if there are any good use cases (except the trivial ones) where current pointer is actually fruitful?
--
Best regards,
speedy mailto:speedy.spam@xxxxxxxxx
.
- Follow-Ups:
- Re: [PHP] nested, referenced foreach & implicit current array pointer issues
- From: "Martin Alterisio"
- Re: [PHP] nested, referenced foreach & implicit current array pointer issues
- Prev by Date: TR: Manipulating "categories" in a php database
- Next by Date: Manipulating "categories" in a mysql database
- Previous by thread: TR: Manipulating "categories" in a php database
- Next by thread: Re: [PHP] nested, referenced foreach & implicit current array pointer issues
- Index(es):