I recently came across a situation where a few of my setProperty() methods where exactly the same. Since I’ve gotten into improving my coding, this bugged me because it was repetitious code.
Example:
public function setFoo($foo=null) {
if (is_string($foo)||is_numeric($foo)) {
$this->foo=$foo;
return $this;
}
throw new Custom_Example_Exception("Please provide a proper string or numeric",Custom_Example_Exception::INVALID_TYPE);
}
public function setBar($bar=null) {
if (is_string($bar)||is_numeric($bar)) {
$this->bar=$bar;
return $this;
}
throw new Custom_Example_Exception("Please provide a proper string or numeric",Custom_Example_Exception::INVALID_TYPE);
}
Notice a pattern there? Me too. If all my setters were like this, I could do
public function setProperty($property=null) {
if (is_string($property)||is_numeric($property)) {
$this->{$property}=$property;
return $this;
}
throw new Custom_Example_Exception("Please provide a proper string or numeric",Custom_Example_Exception::INVALID_TYPE);
}
with appropriate traps in the __call() and maybe even __set() methods.
However, my situation is that not all the setters for my class follow this pattern so that solution means that I have to make __call() look for specific setters.
public function __call($method,$args) {
$trapThese=array("Foo","Bar","Baz");
preg_match('/set(*.?)/im',$method,$matches);
if (!empty($matches)&&in_array($matches[1],$trapThese)) {
if(is_string($args[0])||is_numeric($args[0])) {
$propertyName=strtolower($matches[1]);
$this->{$propertyName}=$args[0];
}
throw new Custom_Example_Exception("Please provide a proper string or numeric.\nException trapped in call to $method",Custom_Example_Exception::INVALID_TYPE);
} else {
$this->{$method}($args);
}
}
And I’m pretty sure that this code snippet ($this->{$method}($args);) is the Wrong Way of Doing It. So I’ll just have to settle for the redundant setFoo(),setBar,etc even though a part of me knows that there has to be a Better Way. Or maybe there is just No Way of Doing It Well.
I think it is probably only a matter of preference but in terms of testability, elegance, maintainability, whatever…what do you prefer, doing a magic __call() or making setFoo(),setBar(),setBaz,etc.