Hi all. Been fiddling with an idea about combining setters and getters in PHP. I really don't like magic methods (__get
and __set
) as they're not very flexible (IMO), but equally bore myself to distraction with set/get methods. Anyhow, having used jQuery for some time, I really liked the non-verbose methods of setting and getting in the client code:
var p = myDiv.html(); //getting
myDiv.html("Hi all"); //setting
So I came up with this as an example:
public function producers( $producers = null )
{
if(func_num_args() > 0){
$this->_producers = $producers;
return $this;
}else{
return $this->_producers;
}
}
It uses the func_num_args to decide whether to set or get. Looking for some feedback. I realise this may not be unique and tried before and the function does do 2 different things. Is this a valid approach or would it bring down the walls of outrage if I placed it in some client code?