2010-10-30 45 views
2

我正在学习Zend Framework,并遇到以下语法。方法语法“public function direct(){}”在PHP中如何工作?

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框架在这个类的直接()方法可使用以下语法可以称为:

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

当重定向器是一个对象在_helper对象,它是内部的(!)控制器对象,我们称之为方法。这里的语法糖是,你可以传递参数的对象,而不是这种方法,我们会写像这样:

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

..这是所有罚款和花花公子ofcourse。

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

谢谢!

回答

9

这是Zend框架的功能。

Controller实例中的$_helpers属性包含Action_HelperBroker实例。这个实例实现了PHP's magic __call method。当您调用该实例中不存在的方法时,它将尝试使用方法名称来获取同名的帮助器,并在其上调用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); 
} 

助手经纪人也实现了神奇的__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'); 

往往是更快的选择。

+3

此外,你可以在这里阅读关于助手(和'直接'方法):http://devzone.zend.com/article/3350 – Ernest 2010-10-30 12:54:28

+0

这是一些很好的代码。感谢您指出! – 2010-10-30 12:56:11

+0

编辑完成后:您谈论的是更长的语法作为更快的替代方案,我们谈论得更快还是可以忽略不计?好的,这当然取决于它,但它就像解析双引号字符串与变量或连接单引号字符串和变量的比较? – 2010-10-30 13:10:39