开发者

Zend Quick Start Guide - Create Model - Why do we need this setOption method?

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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜