François Laupretre wrote on 16/09/2015 13:43:
Le 16/09/2015 14:23, Rowan Collins a écrit :
I would say encouraging devs to treat unset($foo) and $foo=null as
distinct states is an extremely bad idea.
I don't see it this way but, if unset and null states should
absolutely be considered the same, why don't we add
array_key_isset()/property_isset() and deprecate the xx_exists()
functions ?
Because the existence of an array key is a dynamic property of that
array, and is a perfectly reasonable thing to check.
More : why don't we modify the core so that an unset variable returns
null, instead of an error ?
It does return null; it also triggers a warning that you might want to
improve your code by explicitly initialising the variable. It does not
produce an error.
What I don't understand is why considering unset and null as distinct
states 'is is an extremely bad idea'. Don't get me wrong, I really
don't see the potential problems.
The problem is that you're assigning meaning to a state that is
extremely hard to work with. For instance, you can't refactor code which
relies on dynamic variable existence into multiple functions, because
the input parameter of the new function will always exist:
...
unset($foo);
refactored_code($foo);
...
function refactored_code($bar)
{
if exists($bar) // oops, this is always true
...
}
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php