Home  >  Article  >  Backend Development  >  How to remove the behavior bound to a component in PHP's Yii framework_php tips

How to remove the behavior bound to a component in PHP's Yii framework_php tips

WBOY
WBOYOriginal
2016-05-16 19:56:231009browse

To remove a behavior, you can call the yiibaseComponent::detachBehavior() method using the name associated with the behavior:

$component->detachBehavior('myBehavior1');

You can also remove all behaviors:

$component->detachBehaviors();

The above two methods will call yiibaseBehavior::detach(), the code is as follows:

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;
  }
}

Contrary to yiibaseBehavior::attach(), the process of unblocking is to do two things: First, set $owner to null , indicating that this behavior is not attached to any class. The second is to release the event handler bound to the class through Component's off(). In a word, start well and end well.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn