Page 1 of 1

isset and NULL

Posted: Tue Aug 21, 2007 8:19 am
by shiznatix
Ok so I have always used isset to check if a variable exists. But I have just now figured out that a null variable is not set. So if I do:

Code: Select all

class foo
{
    private $var;

    public function __construct()
    {
        echo (isset($this->var) ? 'yes!' : 'nope');
    }
}
this will always show 'nope' when I want it to say 'yes!' because it exists there in the class. Through the use of get_class_vars, get_class, and array_key_exists I can figure it out for sure regardless of the value of $var but this is supra overkill when I thought I could always just use isset.

Is there a function that will give me what I oh so want?

Posted: Tue Aug 21, 2007 8:24 am
by feyd
Unfortunately, no. You will need to go through array_key_exists(). It's not overkill.

edit: oops, forgot "not."

Posted: Tue Aug 21, 2007 8:25 am
by superdezign

Posted: Tue Aug 21, 2007 8:41 am
by CoderGoblin
In some instances empty may be of use. Be aware that an empty string or 0 are both counted as empty.

Posted: Tue Aug 21, 2007 9:09 am
by superdezign
CoderGoblin wrote:In some instances empty may be of use. Be aware that an empty string or 0 are both counted as empty.
Then how would it be useful in this case...?

Posted: Tue Aug 21, 2007 9:28 am
by CoderGoblin
true.. misread question... :oops:

Posted: Tue Aug 21, 2007 11:22 am
by RobertGonzalez
I like supers suggestion.

Posted: Tue Aug 21, 2007 12:22 pm
by feyd
Everah wrote:I like supers suggestion.
It's a shame the function is available in 5.1+ ;)

Posted: Tue Aug 21, 2007 12:28 pm
by RobertGonzalez
True, but we should all be pushing for PHP 5 from our hosts anyway...

I agree though. I forget sometimes that some developers are not as spoiled as me and get to develop in the latest and greatest.

Posted: Wed Aug 22, 2007 2:43 am
by shiznatix
I went with the property_exists function. Its good if you are using PHP5 and going OOP style but if you are doing things procedurally then I think you might be a bit screwed.

Posted: Wed Aug 22, 2007 3:24 am
by stereofrog
I'm wondering why you would need this (except for debugging).

Posted: Wed Aug 22, 2007 5:45 am
by shiznatix
stereofrog wrote:I'm wondering why you would need this (except for debugging).

Code: Select all

if (!property_exists(get_class($this), 'id'))
    	{
    		if (!empty($this->_id))
    		{
    			$use_id = $this->_id;
    		}
    		else
    		{
    			trigger_error('Can not save without an id', E_USER_ERROR);
    			
    			return false;
    		}
    	}
    	else
    	{
    		$use_id = 'id';
    	}
edit: to explain better, this is part of my save method for saving entries to the db. Since I always use a field called 'id' for my autoincrement table fields I don't have a problem but since I have to update some vBulletin tables and they use things like userid, threadid, etc I have to tell my script what field to update. Example of what my table queries look like:

Code: Select all

UPDATE table_name SET foo = "bar" WHERE id = "24"
but a VB update would be like:

Code: Select all

UPDATE table_name SET foo = "bar" WHERE userid = "33"
so I have to let my script know that I am not updating on the id but instead on the userid.

Posted: Wed Aug 22, 2007 7:27 am
by superdezign
I don't think you need to use get_class().

Posted: Wed Aug 22, 2007 8:08 am
by shiznatix
superdezign wrote:I don't think you need to use get_class().
yes you do. it needs a string name of the class you are in

Posted: Wed Aug 22, 2007 8:26 am
by superdezign
No, it doesn't. It *needs* a string name of the property you are looking for.
http://php.net/property-exists wrote:Parameters

class
The class name or an object of the class to test for

property
The name of the property