要移除行為,可以調(diào)用 yii/base/Component::detachBehavior() 方法用行為相關(guān)聯(lián)的名字實現(xiàn):
$component->detachBehavior('myBehavior1');
也可以移除全部行為:
$component->detachBehaviors();
這上面兩種方法,都會調(diào)用到 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);
- //Vevb.com
- }
- $this->owner = null;
- }
- }
與 yii/base/Behavior::attach() 相反,解除的過程就是干兩件事: 一是將 $owner 設(shè)置為 null ,表示這個行為沒有依附到任何類上。 二是通過Component的 off() 將綁定到類上的事件hanlder解除下來。一句話,善始善終。
新聞熱點
疑難解答