Zend 快速入门指南 - 创建模型 - 为什么我们需要这个 setOption 方法?

发布于 2024-11-03 16:10:23 字数 1736 浏览 1 评论 0原文

从这里的文档: http://framework.zend.com/manual/en/learning .quickstart.create-model.html

我们可以看到:

// application/models/Guestbook.php

class Application_Model_Guestbook
{

    protected $_comment;
    protected $_created;
    protected $_email;
    protected $_id;


    public function __construct(array $options = null)
    {
        if (is_array($options)) {

            $this->setOptions($options);
        }

    }


    public function __set($name, $value)
    {
        $method = 'set' . $name;
        if (('mapper' == $name) || !method_exists($this, $method)) {

            throw new Exception('Invalid guestbook property');
        }

        $this->$method($value);
    }


    public function __get($name)
    {
        $method = 'get' . $name;
        if (('mapper' == $name) || !method_exists($this, $method)) {
            throw new Exception('Invalid guestbook property');

        }
        return $this->$method();
    }

    public function setOptions(array $options)
    {
        $methods = get_class_methods($this);
        foreach ($options as $key => $value) {
            $method = 'set' . ucfirst($key);
            if (in_array($method, $methods)) {
                $this->$method($value);
            }
        }
        return $this;
    }

... following getters and setters...

我不明白,也没有解释(可能是因为它太简单了),它是做什么的,以及为什么我们需要setOptions 方法?

我正在尝试遵循本指南,但我不能在不知道代码存在原因的情况下盲目粘贴代码。我会对仅包含 getter 和 setter 的模型感到满意,但是,如果我不使用此 setOptions 方法,也许这一切都不起作用。我很担心,因为我在构造函数上看到了这一点,所以它一定很重要。

谁能帮我弄清楚,如果我们真的需要这个,如果是的话,这意味着什么?

提前致谢。

From the documentation here:
http://framework.zend.com/manual/en/learning.quickstart.create-model.html

We can see:

// application/models/Guestbook.php

class Application_Model_Guestbook
{

    protected $_comment;
    protected $_created;
    protected $_email;
    protected $_id;


    public function __construct(array $options = null)
    {
        if (is_array($options)) {

            $this->setOptions($options);
        }

    }


    public function __set($name, $value)
    {
        $method = 'set' . $name;
        if (('mapper' == $name) || !method_exists($this, $method)) {

            throw new Exception('Invalid guestbook property');
        }

        $this->$method($value);
    }


    public function __get($name)
    {
        $method = 'get' . $name;
        if (('mapper' == $name) || !method_exists($this, $method)) {
            throw new Exception('Invalid guestbook property');

        }
        return $this->$method();
    }

    public function setOptions(array $options)
    {
        $methods = get_class_methods($this);
        foreach ($options as $key => $value) {
            $method = 'set' . ucfirst($key);
            if (in_array($method, $methods)) {
                $this->$method($value);
            }
        }
        return $this;
    }

... following getters and setters...

I don't understand and it's not explained (perhaps because it's to easy), what does it do, and why do we need setOptions method ?

I'm trying to follow this guide, but I can't blindly paste code without knowing the reason for it to exist. I will be happy with a model containing only getters and setters, but, perhaps this all thing will not work if I don't use this setOptions method. I'm concerned because I see this on the constructor so, it must be important somehow.

Can anyone help me out to figure it out, if we really need this, and if so, what does it mean ?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

停顿的约定 2024-11-10 16:10:23

这只是各种 ZF 组件使用的模式。

您可以为许多组件的构造函数提供配置数组(选项)。一般来说,此类可配置组件的 API 包括一个 setOptions 方法,就像您在此处看到的那样。

这只是快速入门中的指南。就我个人而言,我不遵循它,因为我相信模型应该遵循更多特定于任务/域的接口 - 例如,在我看来,留言簿模型应该只接受构造函数中的特定内容,例如留言簿的所有者或这样,并且不允许“通用”选项列表。

This is simply a pattern various ZF components use.

You can provide a configuration array (the options) for the constructor for many components. Generally the API for such configurable components includes a setOptions method like you see there.

This is merely a guideline in the quickstart. Personally I don't follow it, since I believe that models should follow a more task/domain-specific interface - For example, in my opinion a guestbook model should only accept specific things in the constructor, such as the owner of the guestbook or such, and not allow a "generic" list of options.

末が日狂欢 2024-11-10 16:10:23

IMO 制定这种方法是一个很好的做法,因为您可能会得到一个“事物数组”(例如......来自表单),而不是离散变量。在我看来,使用它更好、更易读

$this->setOption($form->getValues());

,然后一一调用它,

$data = $form->getValues();
$this->setName($data['name']);
$this->setSurname($data['surname']);
// ....

但是这个方法应该位于由 Application_Model_Guestbook 扩展的某个父类中,这“我猜”超出了快速启动。当数组中的选项缺少设置器时,提出某种通知也可能是一个好习惯。

IMO it's good practice to make such method, because you're likely to get an "array of things" (say... from form), rather then discrete variables. And it's IMO better and more readable to use

$this->setOption($form->getValues());

then call it one by one

$data = $form->getValues();
$this->setName($data['name']);
$this->setSurname($data['surname']);
// ....

But this method should be located in some parent class that is extended by the Application_Model_Guestbook, which was 'i guess) out of the scope of guickstart. It might also be good practise to raise some kind of notice, when the option in array is missing setter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文