方法语法"public function direct(){}"如何在PHP中工作?

Nie*_*Bom 2 php oop zend-framework syntactic-sugar

我正在学习Zend Framework,并且遇到了以下语法.

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

在Zend Framework中,可以使用以下语法调用此类中的direct()方法:

$this->_helper->redirector('index','index');
Run Code Online (Sandbox Code Playgroud)

其中重定向器是_helper对象中的一个对象(!),它位于控制器对象内部,我们在其中调用该方法.这里的语法糖是你可以只将参数传递给对象而不是方法,我们会这样写:

$this->_helper->redirector->gotoSimple('index','index');
Run Code Online (Sandbox Code Playgroud)

..这一切都很好,花花公子.

这是我的问题:这是OO PHP中的direct()方法标准吗?或者这个功能内置于Zend Framework中?我找不到任何关于此的文件.

谢谢!

Gor*_*don 11

它是Zend Framework的功能构建.

$_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;
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

请注意,魔术方法并不意味着替代适当的API.虽然您可以如上所示使用它们,但调用更详细

$this->_helper->getHelper('redirector')->gotoSimple('index','index');
Run Code Online (Sandbox Code Playgroud)

往往是更快的选择.

  • 另外,你可以在这里阅读帮助器(和`direct`方法):http://devzone.zend.com/article/3350 (3认同)