PHP的Yii框架中移除组件所绑定的行为的方法

来源:互联网 发布:淘宝直播秒杀是真的吗 编辑:程序博客网 时间:2024/05/16 19:06
要移除行为,可以调用 yii\base\Component::detachBehavior() 方法用行为相关联的名字实现:
  1. $component->detachBehavior('myBehavior1');
复制代码
也可以移除全部行为:
  1. $component->detachBehaviors();

复制代码
这上面两种方法,都会调用到 yii\base\Behavior::detach() ,其代码如下:
  1. public function detach()
  2. {
  3. // 这得是个名花有主的行为才有解除一说
  4. if ($this->owner) {
  5. // 遍历行为定义的事件,一一解除
  6. foreach ($this->events() as $event => $handler) {
  7. $this->owner->off($event, is_string($handler) ? [$this,
  8. $handler] : $handler);
  9. }
  10. $this->owner = null;
  11. }
  12. }
1 0
原创粉丝点击