yii\base\Event::off PHP Метод

off() публичный статический Метод

This method is the opposite of Event::on.
См. также: on()
public static off ( string $class, string $name, callable $handler = null ) : boolean
$class string the fully qualified class name from which the event handler needs to be detached.
$name string the event name.
$handler callable the event handler to be removed. If it is `null`, all handlers attached to the named event will be removed.
Результат boolean whether a handler is found and detached.
    public static function off($class, $name, $handler = null)
    {
        $class = ltrim($class, '\\');
        if (empty(self::$_events[$name][$class])) {
            return false;
        }
        if ($handler === null) {
            unset(self::$_events[$name][$class]);
            return true;
        } else {
            $removed = false;
            foreach (self::$_events[$name][$class] as $i => $event) {
                if ($event[0] === $handler) {
                    unset(self::$_events[$name][$class][$i]);
                    $removed = true;
                }
            }
            if ($removed) {
                self::$_events[$name][$class] = array_values(self::$_events[$name][$class]);
            }
            return $removed;
        }
    }

Usage Example

Пример #1
0
 public function testOff()
 {
     $handler = function ($event) {
         $this->counter++;
     };
     $this->assertFalse(Event::hasHandlers(Post::className(), 'save'));
     Event::on(Post::className(), 'save', $handler);
     $this->assertTrue(Event::hasHandlers(Post::className(), 'save'));
     Event::off(Post::className(), 'save', $handler);
     $this->assertFalse(Event::hasHandlers(Post::className(), 'save'));
 }
All Usage Examples Of yii\base\Event::off