我正在學習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中? 我找不到任何文檔。
謝謝!
此外,你可以在這裏閱讀關於助手(和'直接'方法):http://devzone.zend.com/article/3350 – Ernest 2010-10-30 12:54:28
這是一些很好的代碼。感謝您指出! – 2010-10-30 12:56:11
編輯完成後:您談論的是更長的語法作爲更快的替代方案,我們談論得更快還是可以忽略不計?好的,這當然取決於它,但它就像解析雙引號字符串與變量或連接單引號字符串和變量的比較? – 2010-10-30 13:10:39