2

私は現在ZendFrameworkを学んでいて、次の構文に出くわしました。

class Zend_Controller_Action_Helper_Redirector extends Zend_Controller_Action_Helper_Abstract
{
    /**
     * Perform a redirect to an action/controller/module with params
     *
     * @param  string $action
     * @param  string $controller
     * @param  string $module
     * @param  array  $params
     * @return void
     */
    public function gotoSimple($action, $controller = null, $module = null, array $params = array())
    {
        $this->setGotoSimple($action, $controller, $module, $params);

        if ($this->getExit()) {
            $this->redirectAndExit();
        }
    }

    /**
     * direct(): Perform helper when called as
     * $this->_helper->redirector($action, $controller, $module, $params)
     *
     * @param  string $action
     * @param  string $controller
     * @param  string $module
     * @param  array  $params
     * @return void
     */
    public function direct($action, $controller = null, $module = null, array $params = array())
    {
        $this->gotoSimple($action, $controller, $module, $params);
    }
}

Zend Frameworkでは、このクラスのdirect()メソッドは、次の構文を使用して呼び出すことができます。

$this->_helper->redirector('index','index');

ここで、redirectorは、メソッドを呼び出すコントローラーオブジェクト内にある_helperオブジェクト内のオブジェクト(!)です。ここでの構文上の糖衣は、メソッドではなくオブジェクトにパラメーターを渡すことができるということです。これは次のように記述します。

$this->_helper->redirector->gotoSimple('index','index');

..これはもちろんすべて元気でダンディです。

これが私の質問です:このdirect()メソッドはOO PHPの標準ですか?または、この機能はZend Frameworkに組み込まれていますか?これに関するドキュメントが見つかりません。

ありがとう!

4

1 に答える 1

11

これは、ZendFrameworkに組み込まれている機能です。

$_helpersControllerインスタンスのプロパティはインスタンスを保持しAction_HelperBrokerます。このインスタンスは、PHPの魔法の__callメソッドを実装しています。そのインスタンスに存在しないメソッドを呼び出すと、メソッド名を使用して同じ名前のヘルパーをフェッチし、direct()(可能であれば)それを呼び出そうとします。以下のコードを参照してください。

からZend_Controller_Action

/**
 * Helper Broker to assist in routing help requests to the proper object
 *
 * @var Zend_Controller_Action_HelperBroker
 */
protected $_helper = null;

からZend_Controller_Action_HelperBroker

/**
 * Method overloading
 *
 * @param  string $method
 * @param  array $args
 * @return mixed
 * @throws Zend_Controller_Action_Exception if helper does not have a direct() method
 */
public function __call($method, $args)
{
    $helper = $this->getHelper($method);
    if (!method_exists($helper, 'direct')) {
        require_once 'Zend/Controller/Action/Exception.php';
        throw new Zend_Controller_Action_Exception('Helper "' . $method . '" does not support overloading via direct()');
    }
    return call_user_func_array(array($helper, 'direct'), $args);
}

Helper Brokerはマジック__getメソッドも実装しているため、存在しないプロパティにアクセスしようとすると、ブローカーはプロパティ名を引数として使用します。getHelper()

/**
 * Retrieve helper by name as object property
 *
 * @param  string $name
 * @return Zend_Controller_Action_Helper_Abstract
 */
public function __get($name)
{
    return $this->getHelper($name);
}

マジックメソッドは、適切なAPIの代わりになるものではないことに注意してください。上記のように使用できますが、より詳細に呼び出すと

$this->_helper->getHelper('redirector')->gotoSimple('index','index');

多くの場合、はるかに高速な代替手段です。

于 2010-10-30T12:51:25.187 に答える