方法语法“public function direct(){}"是怎么来的?在 PHP 中工作? [英] How does the method syntax "public function direct(){}" work in PHP?

查看:74
本文介绍了方法语法“public function direct(){}"是怎么来的?在 PHP 中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm learning Zend Framework at the moment and came across the following syntax.

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() 方法:

In Zend Framework the direct() method in this class can be called using the following syntax:

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

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

Where redirector is an object(!) in the _helper object, which is inside the controller object, in which we call the method. The syntactic sugar here is that you can just pass parameters to the object instead of to the method, which we would write like so:

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

..当然,这一切都很好,很花哨.

..which is all fine and dandy ofcourse.

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

Here's my question: is this direct() method standard in OO PHP? Or is this functionality built into Zend Framework? I can't find any documentation on this.

谢谢!

推荐答案

这是 Zend Framework 中内置的功能.

It is functionality build into Zend Framework.

Controller 实例中的 $_helpers 属性包含一个 Action_HelperBroker 实例.这个实例实现了PHP 的魔法__call 方法.当您调用该实例上不存在的方法时,它会尝试使用该方法名称来获取同名的帮助程序并对其调用 direct()(如果可能).请参阅下面的代码.

The $_helpers property in the Controller instance holds an Action_HelperBroker instance. This instance implements PHP's magic __call method. When you call a method that does not exist on that instance, it will try to use the method name to fetch a helper of the same name and call direct() on it (if possible). See code below.

来自 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 方法,所以当你尝试访问一个不存在的属性时,broker 会使用属性名称作为 getHelper() 的参数

The Helper Broker also implement the magic __get method, so when you try to access a property that does not exist, the broker will use the property name as an argument to 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.虽然您可以如上所示使用它们,但调用更详细的

Please be aware that magic methods are not meant as a replacement to a proper API. While you can use them as shown above, calling the more verbose

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

通常是更快的选择.

这篇关于方法语法“public function direct(){}"是怎么来的?在 PHP 中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆