François Laupretre wrote on 23/09/2015 00:11:
So, I thought this was going further and further away from the
original idea, which was asking for 'an equivalence of isset() which
would consider every value, *including* null, as set, because that's
what a lot of devs intuitively assume when using isset()'. That's why
I reverted to a unique exists() construct, whose scope is exactly the
same as isset().
IMHO, that's the best solution, but anybody is free to provide another
patch/implementation.
Fair enough, but for what it's worth, I had *deliberately* moved away
from that original idea, because I still don't agree it's valid, and
we've had nearly a month of discussion in this thread getting to the
bottom of what people's actual requirements are.
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.
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.
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.
- 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.
- isset() handles string offsets.
What useful functionality could exists() have here?
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.
Regards,
--
Rowan Collins
[IMSoP]