Craig Francis wrote on 26/08/2015 18:07:
I provide examples to help explain that I (and I suspect most developers)
default to using isset which works on either.
Just because there is a function, which does not exactly roll off the tongue
(plus the fun of the needle/haystack ordering issue), does not mean it gets
used (even if it is the correct thing to use).
That's all very well, but what is the Internals community supposed to do
about it? Does the documentation need to be improved to point out that
there is a better function to use for this case?
I say "or key" because developers use the isset function on both $var and
$var['key']... yes, I know they are different, but when you are coding in PHP an isset
check is an isset check (well it isn't, because the variable may technically exist).
If this is a problem, maybe PHP should raise a notice when isset is used on
arrays?
It's only a problem in the same way that using file_exists() is a
problem when you actually wanted the functionality of is_readable() -
both functions exist, and it's not the language's responsibility to
guess which you meant.
where NULL may be a perfectly valid value.
It's not that NULL isn't a valid value; it's that "doesn't exist" isn't a
meaningful state for a variable. It's like checking if the current line number is greater
than 100, it shouldn't mean anything to the compiled program. See the SO answer I linked
earlier for more thought experiments along these lines.
I think you have been spending too much time in C.
Most of the PHP code bases I've worked on set variables to NULL at some point
(and they are not calling unset, because that isn't the intention).
Perhaps my double negative made it unclear, so I will restate: there is
nothing wrong with setting a variable to NULL. There is also nothing
wrong with calling unset() on a plain variable, e.g. to make it obvious
to readers of the code that this variable should not be used below this
point.
There IS something wrong with any code which needs to distinguish which
of those two things happened, at run-time, because such code would be
incredibly fragile and hard to follow.
You could, if you wanted, emulate a boolean variable by using $foo=null
for true, and unset($foo) for false, but why not simply have a boolean
variable?
[Incidentally, I know barely any C, but program PHP for a living.]
Regards,
--
Rowan Collins
[IMSoP]
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php