2010-10-30 47 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