《PHP編程:PHP的Yii框架中移除組件所綁定的行為的方法》要點:
本文介紹了PHP編程:PHP的Yii框架中移除組件所綁定的行為的方法,希望對您有用。如果有疑問,可以聯系我們。
相關主題:YII框架
要移除行為,可以調用 yii\base\Component::detachBehavior() 辦法用行為相關聯的名字實現:PHP編程
$component->detachBehavior('myBehavior1');
也可以移除全部行為:PHP編程
$component->detachBehaviors();
這上面兩種辦法,都會調用到 yii\base\Behavior::detach() ,其代碼如下:PHP編程
public function detach() { // 這得是個名花有主的行為才有解除一說 if ($this->owner) { // 遍歷行為定義的事件,一一解除 foreach ($this->events() as $event => $handler) { $this->owner->off($event, is_string($handler) ? [$this, $handler] : $handler); } $this->owner = null; } }
與 yii\base\Behavior::attach() 相反,解除的過程便是干兩件事: 一是將 $owner 設置為 null ,表示這個行為沒有依附到任何類上. 二是通過Component的 off() 將綁定到類上的事件hanlder解除下來.一句話,善始善終.PHP編程
維易PHP培訓學院每天發布《PHP編程:PHP的Yii框架中移除組件所綁定的行為的方法》等實戰技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培養人才。