Rasmus Lerdorf wrote:
We can't just randomly reset variables based on their scope in this one
specific case.  If we are going to "fix" this, it should be done by
introducing a way to do proper local scope variables.  Resetting a
reference simply because it is convenient in this one case would be
completely inconsistent.

-Rasmus

I have some thoughts on this for PHP 6, but here's something else that might be worth looking into.

When you write [foreach ($ii as $i) ...], you usually expect that $i is not a reference variable (you would have used &$i otherwise). However, PHP allows this even if $i is already a reference variable (e.g.: it was used as such in a previous loop).

Now look at this code:

function f(&$x) {
  echo $x;
}
$x = 1;
$args = array($x);
call_user_func_array('f', $args);

In PHP 5.3, this would trigger a warning and cause $x to be NULL inside of f because you mixed value and reference types in an improper way.

This foreach() issue is essentially being caused by a similar evil mix of reference and value types. Perhaps [foreach ($ii as $i)] should trigger a warning if $i already exists as a reference variable.

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to