Page 1 of 1

Setter problem

Posted: Tue Apr 06, 2004 9:41 am
by Nay
I was writing my setter method for a class and came across to write:

Code: Select all

function setVar($varname, $newvalue) {
       // return(void)
		// $varname = The name of a variable to be changed
		// $newvalue = The new value for the variable

		if( emtpy($varname) || empty($newvalue) || !isset($this->class->$newvalue) ) {
			// If given arguments are empty, or the variable name does not exist
			return false;
		} else {
			$this->object->$varname = $newvalue;
			return true;
		}
	}
The thing is I wonder when If I need to set a variable value to 0. Then the $newvalue would come as 0 and checking with empty would return false.

Any ideas?

Thanks,

-Nay

Posted: Tue Apr 06, 2004 11:51 am
by TheBentinel.com
Why do you need to check if the value is empty? Would it be ok to let it go on through regardless?

If 0 is always acceptable, you could change your middle check to:
... || ( empty($newvalue) && $newvalue != 0 ) || ...

I think that would do it.