加入收藏 | 设为首页 | 会员中心 | 我要投稿 汽车网 (https://www.0577qiche.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2023-10-10 11:20:52 所属栏目:PHP教程 来源:
导读:要移除行为,可以调用 yii/base/Component::detachBehavior() 方法用行为相关联的名字实现:

$component->detachBehavior('myBehavior1');

也可以移除全部行为:

$component->detachBehaviors();
要移除行为,可以调用 yii/base/Component::detachBehavior() 方法用行为相关联的名字实现:

$component->detachBehavior('myBehavior1');

也可以移除全部行为:

$component->detachBehaviors();

这上面两种方法,都会调用到 yii/base/Behavior::detach() ,其代码如下:

public function detach(){ 
 
  // 这得是个名花有主的行为才有解除一说 
 
  if ($this->owner) { 
 
    // 遍历行为定义的事件,一一解除 
 
    foreach ($this->events() as $event => $handler) { 
 
      $this->owner->off($event, is_string($handler) ? [$this, 
 
        $handler] : $handler); 
//Cuoxin.com 
    } 
 
    $this->owner = null; 
 
  } 
 

与 yii/base/Behavior::attach() 相反,解除的过程就是两件事:一是将 $owner 设置为 null ,表示这个行为没有依附到任何类上。 二是通过Component的 off() 将绑定到类上的事件hanlder解除下来。一句话,善始善终。

(编辑:汽车网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章