Good morning,
Perhaps a future version of PHP (PHP 6? 5.7?) could have lexical scoping with a
"let" keyword? At the very least, it would make foreach() with a reference less
of a problem:
foreach ($array as let &$a) {
$a = 3;
}
// $a is not in this scope - no need to worry about accidentally changing
last array item
Any thoughts?
Andrea Faulds
http://ajf.me/
2014-01-28 Andrea Faulds ajf@ajf.me
Good morning,
Perhaps a future version of PHP (PHP 6? 5.7?) could have lexical scoping
with a
"let" keyword? At the very least, it would make foreach() with a reference
less
of a problem:foreach ($array as let &$a) { $a = 3; } // $a is not in this scope - no need to worry about accidentally
changing
last array itemAny thoughts?
Hi,
My 2 cents: That is a very minor improvement at the cost of an additional
keyword. It is only to avoid, that you accidentally interfere with outers
scope variables? If it is hard to track these variables, maybe your
method/function is too big.
Regarding your example:
foreach ($array as &$a) {
$a = 3;
}
$a = null; // $a not a reference anymore. Problem solved :)
Regards,
Sebastian
--
Andrea Faulds
http://ajf.me/--
2014-01-28 Sebastian Krebs krebs.seb@gmail.com
2014-01-28 Andrea Faulds ajf@ajf.me
Good morning,
Perhaps a future version of PHP (PHP 6? 5.7?) could have lexical scoping
with a
"let" keyword? At the very least, it would make foreach() with a
reference
less
of a problem:foreach ($array as let &$a) { $a = 3; } // $a is not in this scope - no need to worry about accidentally
changing
last array itemAny thoughts?
Hi,
My 2 cents: That is a very minor improvement at the cost of an additional
keyword. It is only to avoid, that you accidentally interfere with outers
scope variables? If it is hard to track these variables, maybe your
method/function is too big.
I agree.
Regarding your example:
foreach ($array as &$a) {
$a = 3;
}
$a = null; // $a not a reference anymore. Problem solved :)
Problem not solved. You have just changed the last element of $array...
This is the exact problem that was to be avoided.
Lazare INEPOLOGLOU
Ingénieur Logiciel
Regards,
Sebastian--
Andrea Faulds
http://ajf.me/--
Regarding your example:
foreach ($array as &$a) {
$a = 3;
}
$a = null; // $a not a reference anymore. Problem solved :)Problem not solved. You have just changed the last element of $array...
This is the exact problem that was to be avoided.
unset($a)
after the foreach solves the problem.
2014-01-28 Sebastian Krebs krebs.seb@gmail.com
2014-01-28 Andrea Faulds ajf@ajf.me
Good morning,
Perhaps a future version of PHP (PHP 6? 5.7?) could have lexical
scoping
with a
"let" keyword? At the very least, it would make foreach() with a
reference
less
of a problem:foreach ($array as let &$a) { $a = 3; } // $a is not in this scope - no need to worry about accidentally
changing
last array itemAny thoughts?
Hi,
My 2 cents: That is a very minor improvement at the cost of an additional
keyword. It is only to avoid, that you accidentally interfere with outers
scope variables? If it is hard to track these variables, maybe your
method/function is too big.I agree.
Regarding your example:
foreach ($array as &$a) {
$a = 3;
}
$a = null; // $a not a reference anymore. Problem solved :)Problem not solved. You have just changed the last element of $array...
This is the exact problem that was to be avoided.
AFAIK, this is a problem which is quite limited to foreach() (are there
other examples?), so one (BC-breaking-but-probably-for-the-best) solution
would simply be to unset the variable automatically if it wasn't already
defined:
foreach( $array as &$a ){
$a = 3;
}
var_dump( $a ); // E_NOTICE, $a is now undefined
$b = 2;
foreach( $array as &$b ){
$b = 3;
}
var_dump( $b ); // 3, $b was defined before we started the foreach, so it's
not wiped
Alternatively, you could say that the ultimate problem is that we don't
have strong scoping - braces only define flow control, not scope. So you
could introduce a strong-scoping structure. Maybe a double brace?
foreach( $array as &$a ){{
$a = 3;
}}
var_dump( $a ); // E_NOTICE, $a is now undefined
You could even say that an isolated brace pair (ie not one following a
control statement) always produces a scope.
--GB
Alternatively, you could say that the ultimate problem is that we don't
have strong scoping - braces only define flow control, not scope. So you
could introduce a strong-scoping structure. Maybe a double brace?foreach( $array as &$a ){{
$a = 3;
}}
var_dump( $a ); // E_NOTICE, $a is now undefinedYou could even say that an isolated brace pair (ie not one following a
control statement) always produces a scope.
My proposal is to add optional strong scoping. As JavaScript has var for
function-scoped variables and ECMAScript Harmony will add let for
lexically-scoped variables, I'm proposing something similar here. It
would allow you to scope to the block, if you so desire.
--
Andrea Faulds
http://ajf.me/
2014-01-28 Lazare Inepologlou linepogl@gmail.com
2014-01-28 Sebastian Krebs krebs.seb@gmail.com
2014-01-28 Andrea Faulds ajf@ajf.me
Good morning,
Perhaps a future version of PHP (PHP 6? 5.7?) could have lexical scoping
with a
"let" keyword? At the very least, it would make foreach() with a
reference
less
of a problem:foreach ($array as let &$a) { $a = 3; } // $a is not in this scope - no need to worry about accidentally
changing
last array itemAny thoughts?
Hi,
My 2 cents: That is a very minor improvement at the cost of an additional
keyword. It is only to avoid, that you accidentally interfere with outers
scope variables? If it is hard to track these variables, maybe your
method/function is too big.I agree.
Regarding your example:
foreach ($array as &$a) {
$a = 3;
}
$a = null; // $a not a reference anymore. Problem solved :)Problem not solved. You have just changed the last element of $array...
This is the exact problem that was to be avoided.
Touche ;)
then use unset(). This time I tried it before:
http://codepad.viper-7.com/lOx1XL
Lazare INEPOLOGLOU
Ingénieur LogicielRegards,
Sebastian--
Andrea Faulds
http://ajf.me/--
foreach ($array as &$a) {
$a = 3;
}
$a = null; // $a not a reference anymore. Problem solved :)
You just changed the last element of the array to null.
See why I proposed this? ;)
Andrea Faulds
http://ajf.me/
2014-01-28 Andrea Faulds ajf@ajf.me
foreach ($array as &$a) {
$a = 3;
}
$a = null; // $a not a reference anymore. Problem solved :)You just changed the last element of the array to null.
See why I proposed this? ;)
I already fixed this... Read the other messages as well:
http://codepad.viper-7.com/lOx1XL
--
Andrea Faulds
http://ajf.me/
Hi!
Perhaps a future version of PHP (PHP 6? 5.7?) could have lexical scoping with a
"let" keyword? At the very least, it would make foreach() with a reference less
of a problem:foreach ($array as let &$a) { $a = 3; } // $a is not in this scope - no need to worry about accidentally changing
last array item
Typing unset($a) takes about 3 secs. Since you need to make additions
anyway, and you can already easily do it in the language - I don't see
any need for introducing new syntax. The use case for it seems pretty
small and already served by unset().
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227