Le 23/09/2015 12:18, Rowan Collins a écrit :
Clearly, people misunderstand what isset() does, but I think an exists()
function which mimics it with a slight difference will only add to that
confusion - people will say "oh, if only I had PHP 7.1" rather than
looking for array_key_exists, for instance.
Yes, isset() name is misleading and exists() implements the behavior
most people actually expect from isset(). If a side effect is to
motivate people to adopt PHP 7, why not ? If they start replacing calls
to isset()/array_key_exists() with exists(), that's their choice and it
can only improve overall readability. My main concern is people using
isset($foo['bar']) and 'naively' assuming it always returns true when
the array element exists, because wrong assumptions are always
potentially problematic. The rest is just a question of completeness and
consistency(). IMO, providing an exists() construct is easy to
understand and to remember, quite consistent as the scope is the same as
isset(), and the drawbacks are minimal (including BC break).
Regarding the confusion it may bring, the documentation can be very
clear, stating that the right tool to check for the existence of an
array element in pre-7.1 versions is array_key_exists(), not isset().
So the purpose of hasitem() was not to be a variant of isset(), but to
be a well-defined function which served a specific need that people
have. If there are other needs that people have, there can be other
functions to serve them.
1) hasitem cannot be implemented as a function. It needs to be defined
in the lexer/parser the same way as exists(), or do you mean it would
receive a string ? Anyway, if you want to allow 'hasitem($foo['bar'])',
it cannot be a function like property_exists()/functions_exists().
2) it defines 2 names instead of 1,
3) the 'hasitem' name is not extremely intuitive,
4) it is more complex to implement and I'm not sure I can do it :)
5) I don't see the reason to make it artificially more complex and
harder to remember.
Looking at some of your edge-cases:
- array_key_exists() does not handle objects implementing ArrayAccess,
Sounds like something which could be fixed in array_key_exists, or
easily added to hasitem() without all the other baggage.
Possible, yes.
- property_exists() does not handle 'magic' properties (those
controlled by magic methods),
exists() cannot meaningfully handle these either, without an additional
magic method; or, it will call __isset(), and return the same value as
isset(), which seems a bit pointless.
Yes, in this case, exists() calls __isset(), and returns the same value
as isset(). It may seem pointless but, unlike static properties, the
logic to consider virtual properties as 'set' or 'unset' is free and
under the responsibility of the developer. He can decide that a 'null'
property is 'set' or not. We just relay the information.
- isset() handles string offsets.
What useful functionality could exists() have here?
Probably none but isset() supports it and I want the same scope...
Note: A variable_exists() function accepting a string wouldn't be more
powerful, as exists() provides the same feature when using a '$$'
variable variable.
OK, that's not exactly intuitive or easy to use though. If you're
checking for a variable's existence, you've presumably dynamically
created that variable, so you're quite likely to want the argument to
variable_exists() to also be dynamic. Again, variable_exists() would be
a function which had a well-defined scope, and obvious how to use it.
??
Variable variables provide dynamic names :
$var='var_to_check_'.$i;
if (exists($$var)) echo "$i: $$var";
IMO, that's not much harder than using variable_exists(<string>).
Regards
François
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php