isset and NULL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

isset and NULL

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Unfortunately, no. You will need to go through array_key_exists(). It's not overkill.

edit: oops, forgot "not."
Last edited by feyd on Tue Aug 21, 2007 8:28 am, edited 1 time in total.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

In some instances empty may be of use. Be aware that an empty string or 0 are both counted as empty.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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...?
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

true.. misread question... :oops:
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I like supers suggestion.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Everah wrote:I like supers suggestion.
It's a shame the function is available in 5.1+ ;)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

I'm wondering why you would need this (except for debugging).
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

I don't think you need to use get_class().
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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
Post Reply