Setter problem

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
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Setter problem

Post 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
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

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