
Originally Posted by
BerislavLopac
I use the plain properties, but if a property has to be worked on before returning I prefer the magic methods.
Are you referring to the __set magic method or __call magic method?
I ask, because you can use either. For example, let's say the __set magic method were constructed like so:
Code:
public function __set($key, $value)
{
$method = 'set_' . $key;
if (method_exists($this, $method)) {
$this->$method($value);
} else {
$this->data[$key] = $value;
}
}
You could have a set_something($something) method, which perform pre-processing/filtering:
Code:
public function set_something($something)
{
$this->data['something'] = strtoupper($something);
}
Then setting a class property like so:
Code:
$object->something = 'uppercase me, then set me';
would be intercepted by the __set magic method, which would silently call the set_something() method, which uppercase, then sets "something".
Is anyone employing this usage of __set or the other magic methods in order to eliminate get_*, or set_* methods, even those dynamically called by the __call magic method?
Bookmarks