On Tue, Aug 19, 2003 at 04:38:03PM -0500, Shaun Thomas wrote:
> On Tue, 19 Aug 2003, fabrice wrote:
> 
> > Empty($var) ? DoSomethingIfNotSetOrNull() : DoOther();
> 
> That won't work.  empty() will return true if the variable is set to a
> literal zero, false, blank, or null.  So for your use, it's actually 
> worse than isset assuming your data can have zeros or blank values.
> 
> A more proper check for a pseudo variable_exists would be something like 
> this:
> 
> if (isset($szSomeString) || is_null($szSomeString))
> {
>   // Stuff here.
> }

Won't work.
[EMAIL PROTECTED]:~$ php -r 'var_dump(isset($var) || is_null($var));'

Notice: Undefined variable:  var in Command line code on line 1
bool(true)
 
> Because as we all know, isset only fails if the variable doesn't exist, 
> or if it's set to null.  So add in a check for null, and you get the 
> same thing as variable_exists.  Or if you're a math person:

Too bad that when you read from an undefined var, you get null also.

> var $nCount = NULL;

That is equivalent to var $nCount; btw...

> This is programmatically equivalent to initializing a variable to an 
> unknown value, since the class has not yet been instantiated.  Good 
> practice is good practice, regardless of what the language provides.  
> Sure I don't have to do this, but it makes for more readable code.
> 
> This is a moot point, of course; I initialized the variable, so calling 
> isset to check for existence would be silly.
> 
> Some people have suggested using array_key_exists when using arrays,
> which is fine as long as you never use the PHP StdClass object which all
> database wrappers may return.  Or you can waste memory by calling
> get_object_vars to treat a StdClass object like an array for the two
> things you wanted to check out of the entire object.

Or, you simply don't use pseudo-objects as nonvalidating data containers, 
because arrays do that job better.

> Actually now that I think about it, the object-level functions are 
> badly crippled compared to the vastness of the array_* routines, 
> foreach, and so on, but that's a different discussion.

The vast majority of array functions doesn't make sense for Objects -
yet. Wait for PHP5.

> So to summarize:
> 
> 1.) variable_exists($sVar) = isset($sVar) || is_null($sVar)

Wrong. I pointed that out.

> 2.) variable_exists can not be written in PHP.

And it is not neccessary, because get_defined_vars() exists.

> 3.) Using nulls to initialize variables is not stupid.

Noone ever said said that. 
Still no need to test for their existance, imo.
If you did $var = null; somewhere, you don't need to test for its
existance anymore.
And even if you did
if (condition()) {
  $var = 'value';
} elseif (other_condition()) {
  $var = null;
} else {
  unset($var);
}

There is no need to check for existance, imo. $var = null; means it has
no value afterwards, so you can just use isset(). There's no value you
could overwrite.

> 4.) Thus, add variable_exists, or deal with annoying double-check
>     on every variable you intend to use this way.

Since your presumptions are flawed, the conclusion is also.
And, given good design, where would you need it?

-- 
Regards,
Stefan Walk
<[EMAIL PROTECTED]>

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to