I'm not really experienced with PHP magic methods, and I'm trying to create implicit getters and setters that interact with Laravel framework. Now, I know there are accessors and mutators , but they have to be explicitly declared. What I would like is to make is some kind of implicit function instead of declaring them. I saw this done in Zend framework, it was something like
public function __call ($method, $params) {
$property = strtolower($this->_getCamelCaseToUnderscoreFilter()->filter(substr($method, 3)));
if (!property_exists($this, $property))
return null;
// Getters
elseif (substr($method, 0, 3) == 'get')
{
return $this->$property;
}
// Setters
elseif (substr($method, 0, 3) == 'set')
{
$this->$property = $params[0];
return $this;
}
else
return null;
}
Now if I have a model with this function, I'll be able to do just $model->getProperty()
or $model->setProperty($property)
.
But I'm not sure how can I apply it to Laravel. any idea?
I created a parent class for all of my Models named "Moam" (as Mother of all Models) In this Moam I've implement those magic methods well known by good old Zend framework.
/app/Models/Moam.php is:
In other models you have to declare your models like this:
Finally you can call setters and getters in form below:
For instance: