Yii 行为是干什么的?

Guanhui
Guanhui 原创
2020-06-10 15:54:30 2296浏览

Yii 行为是干什么的?

Yii行为是事件的升级版,所有的行为都是Behavior的子类,其作用是将相似事件句柄放在一起,在行为执行“attach()”方法的时候会将“events()”方法中返回的事件句柄进行绑定,这样做达到方面管理和扩展的目的。

示例代码

/**
* Raised right BEFORE the application processes the request.
* @param CEvent $event the event parameter
*/
public function onBeginRequest($event)
{
  $this->raiseEvent('onBeginRequest',$event);
}
/**
* Runs the application.
* This method loads static application components. Derived classes usually overrides this
* method to do more application-specific tasks.
* Remember to call the parent implementation so that static application components are loaded.
*/
public function run()
{
  if($this->hasEventHandler('onBeginRequest'))
    $this->onBeginRequest(new CEvent($this));
  $this->processRequest();
  if($this->hasEventHandler('onEndRequest'))
    $this->onEndRequest(new CEvent($this));
}


推荐教程:《PHP教程》《Yii教程

以上就是Yii 行为是干什么的?的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。