Hi,

I've lately discussed with a colleague which scopes of variables exist
for PHP or would probably make sense. In general I think the general
idea of having variables available all throughout a function is okay as
this allows things like

foreach($vals as $v) {
  // ...
  $found = true;
}
if($found) {
  // ...
}

(setting $found inside the loop while still being able to access it outside)

But the interesting part is that $v is also still available outside the
loop (last value). While most people would say this is not a big
problem, it can become problematic when using references.

foreach($vals as &$temp) {
  // ...
}
// ...
$temp = 5;
(when you don't think about the reference anymore but want some
temp-variable)


If this has been "throughly discussed" before, please excuse. But if not
maybe somebody could share his oppinion on the following proposal.

What if we (for example with PHP 5.4 or if not possible maybe with the
next one) change the behaviour so that

* variables used for key/value in foreach (probably other places?) would
be limited to that loop-scope

and maybe
* variable $found in the first example would need to be initialised
before the loop. Otherwise it would be a new variable inside the scope
of foreach that would be gone afterwards

and/or maybe
* allowing to explicitly limit variable-scopes inside blocks, for
example by allowing    var $found   somewhere inside a function to allow
things like

if($a) {
  var $temp;

  $temp = 5;
}
// and $temp would be gone here; was limited to the scope in which it
was defined by var


Hope this is not too much of a non-sense idea to you :-)


Kind regards,
 Stefan

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

Reply via email to