nested, referenced foreach & implicit current array pointer issues

From: Date: Tue, 30 Jan 2007 18:31:49 +0000
Subject: nested, referenced foreach & implicit current array pointer issues
Groups: php.general 
Request: Send a blank email to php-general+get-248043@lists.php.net to get a copy of this message
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@gmail.com

« previous php.general (#248043) next »