简体   繁体   中英

Getters and setters with magic PHP calls Laravel

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:

<?php

namespace App\Models;

use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;

/* Mother of all models*/
class Moam extends Model
{   
    /* Implementation of magical get and set method for manipulation model properties
     * i.e. setFirstName('xxx') sets property first_name to 'xxx'
     *      getFirstName() returns value of property first_name
     *
     * So Capitals replaced underscores (_) in property name
     */

    public function __call($methodName, $args) {
        if (preg_match('~^(set|get)([A-Z])(.*)$~', $methodName, $matches)) {

            $split = preg_split('/(?=[A-Z])/',$methodName);
            $property = strtolower (implode("_",array_slice($split,1)));

            $attributes = $this->getAttributes();
            if (!array_key_exists($property,$attributes)){
                Log::error(get_class($this) . ' has no attribute named "' . $property . '"');
                throw new Exception(get_class($this) . ' has no attribute named "' . $property . '"');
            } else {

                switch($matches[1]) {
                case 'set':
                    return $this->setAttribute($property, $args[0]);
                case 'get':
                    return ($attributes[$property]);
                }
            }
        }

        return parent::__call($methodName, $args);
    }
}

In other models you have to declare your models like this:

use App\Models\Moam;
...

class yourModelextends Moam{
   ...
}

Finally you can call setters and getters in form below:

$modelVariable->getAPropertyName();
$modelVariable->setAPropertyName(propertyValue);

For instance:

$desc = $event->getDescription();
$event->setDescription("This is a blah blah blah...");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM