【问题标题】:Getters and setters with magic PHP calls Laravel使用魔术 PHP 调用 Laravel 的 getter 和 setter
【发布时间】:2014-09-01 09:46:17
【问题描述】:

我对 PHP 魔术方法没有真正的经验,我正在尝试创建与 Laravel 框架交互的隐式 getter 和 setter。现在,我知道有 accessors 和 mutators ,但必须明确声明它们。我想做的是某种隐式函数,而不是声明它们。 我看到这是在 Zend 框架中完成的,类似于

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;
}

现在,如果我有一个具有此功能的模型,我将能够做到 $model->getProperty()$model->setProperty($property)。 但我不确定如何将其应用于 Laravel。有什么想法吗?

【问题讨论】:

  • 你需要这个模型的哪些属性?模型的属性通常包括模型反映的表的列。您还需要哪些附加属性?
  • 只有表格的列。 ]
  • 这让我想到了我原来的问题:你需要这个模型的哪些属性?
  • 我想创建一些抽象类并让我的所有模型都扩展该类,因此,实现这个功能并让所有模型都能够访问它们的所有属性现在,我只需要 id 、电子邮件、名字和姓氏
  • 肯定 id 已经定义(默认情况下它用于每个模型),并且您列出的其他属性肯定只适用于某些模型,它们是模型反映的表中的列跨度>

标签: php laravel laravel-4 magic-function


【解决方案1】:

我为我的所有模型创建了一个父类,名为“Moam”(作为 所有模型之母) 在这个 Moam 中,我实现了古老的 Zend 框架众所周知的魔法方法。

/app/Models/Moam.php 是:

<?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);
    }
}

在其他模型中,您必须像这样声明您的模型:

use App\Models\Moam;
...

class yourModelextends Moam{
   ...
}

最后你可以用下面的形式调用 setter 和 getter:

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

例如:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    • 2010-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多