The magic methods are slower (I'd have to do some benchmarking to give accurate numbers).
For getter/setters I would use the magic methods __get() and __set() (see:
http://php.net/manual/en/language.oop5.magic.php) From your code snippet I take it you are using it like:
$obj->setfoo(5);
$foo = $obj->getfoo();
You could so something like:
class Foo
{
private $data = array();
public function __set($name, $value)
{
$this->data[$name] = $value;
}
public function __get($name)
{
return isset($this->data[$name]) ? $this->data[$name] : null;
}
}
However, IIRC __get and __set don't work on actual properties. So if you tried to do
$obj->data['blah'] = 9;
I think you'll get an error since $data is private.
I actually used __call to handle dynamic error handling. I wanted to be able to either return false or throw an exception and control that at run time. So I prefixed the actual methods and make them protected (so you couldn't call them directly). The calls use the method names without the prefix. Example:
class Foo
{
protected function throwable_bar()
{
echo 'You suck';
}
public function __call($func, $args)
{
$func = "throwable_" . $func;
call_user_func_array($args);
}
I'd have bar do normal exception error handling and __call would catch it (or not). IIRC I had to use
reflection to build a list of functions that can be called that way.