Re: Re: variable_exists() patch
| From: | Lars Torben Wilson | Date: | Thu, 14 Aug 2003 19:32:01 +0000 |
| Subject: | Re: Re: variable_exists() patch | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-3956@lists.php.net to get a copy of this message | ||
On Thu, 2003-08-14 at 08:42, Andi Gutmans wrote:
> I am not really convinced either that variable_exists() is
> function_exists() parallel.
> Under what circumstances is this needed?
>
> Andi
The followup I sent to Ilia gives examples of how this patch can be
used to determine whether, for instance, array keys exist, without
having to use the array_key_exists() workaround; also, you can check
whether an object has defined an attribute, even if it hasn't yet
assigned it a value:
<?php
class foo {
var $bar;
var $baz = null;
var $quux = 'quux';
}
$foo = new foo;
echo "variable_exists(\$foo->bar): " . (variable_exists($foo->bar) ?
'yes' : 'no') . " (should be yes)\n";
echo "variable_exists(\$foo->baz): " . (variable_exists($foo->baz) ?
'yes' : 'no') . " (should be yes)\n";
echo "variable_exists(\$foo->quux): " . (variable_exists($foo->quux)
? 'yes' : 'no') . " (should be yes)\n";
echo "variable_exists(\$foo->quuux): " .
(variable_exists($foo->quuux) ? 'yes' : 'no') . " (should be
no)\n";
echo "isset(\$foo->bar): " . (isset($foo->bar) ? 'yes' :
'no') . "
(should be yes)\n";
echo "isset(\$foo->baz): " . (isset($foo->baz) ? 'yes' :
'no') . "
(should be yes)\n";
echo "isset(\$foo->quux): " . (isset($foo->quux) ? 'yes' :
'no') . "
(should be yes)\n";
echo "isset(\$foo->quuux): " . (isset($foo->quuux) ? 'yes' :
'no') .
" (should be no)\n";
?>
...the output from the above is:
-----------------------------------------------------------------------
variable_exists($foo->bar): yes (should be yes)
variable_exists($foo->baz): yes (should be yes)
variable_exists($foo->quux): yes (should be yes)
variable_exists($foo->quuux): no (should be no)
isset($foo->bar): no (should be yes)
isset($foo->baz): no (should be yes)
isset($foo->quux): yes (should be yes)
isset($foo->quuux): no (should be no)
-----------------------------------------------------------------------
etc. It's a little annoying at times that that isset() doesn't do what
its name suggests it should, which leads people to try to use it
for things which is seems that it should be able to do, but can't. When
I've tried to explain it, the only answer I can give is "Sorry, that's
just the way it is--and no, you can't do that in PHP".
It seems basic enough functionality that is almost, but not quite,
satisfied by isset().
--
Torben Wilson <torben@php.net> +1.604.709.0506
http://www.thebuttlesschaps.com http://www.inflatableeye.com
http://www.hybrid17.com http://www.themainonmain.com
-----==== Boycott Starbucks! http://www.haidabuckscafe.com ====-----