search
Homephp教程php手册PHP的Yii框架中移除组件所绑定的行为的方法,yii框架

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

要移除行为,可以调用 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);
    }
    $this->owner = null;
  }
}

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

您可能感兴趣的文章:

  • PHP的Yii框架中行为的定义与绑定方法讲解
  • 详解在PHP的Yii框架中使用行为Behaviors的方法
  • 深入讲解PHP的Yii框架中的属性(Property)
  • PHP的Yii框架中使用数据库的配置和SQL操作实例教程
  • 深入解析PHP的Yii框架中的event事件机制
  • 全面解读PHP的Yii框架中的日志功能
  • Yii使用find findAll查找出指定字段的实现方法
  • 解析yii数据库的增删查改
  • Yii PHP Framework实用入门教程(详细介绍)
  • 详解PHP的Yii框架中组件行为的属性注入和方法注入
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools