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

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

When a class-level event is triggered, event handlers attached to that class and all parent classes will be invoked. For example, the following code attaches an event handler to ActiveRecord's afterInsert event: php Event::on(ActiveRecord::className(), ActiveRecord::EVENT_AFTER_INSERT, function ($event) { Yii::trace(get_class($event->sender) . ' is inserted.'); }); The handler will be invoked for EVERY successful ActiveRecord insertion. For more details about how to declare an event handler, please refer to [[Component::on()]].
См. также: off()
public static on ( string $class, string $name, callable $handler, mixed $data = null, boolean $append = true )
$class string the fully qualified class name to which the event handler needs to attach.
$name string the event name.
$handler callable the event handler.
$data mixed the data to be passed to the event handler when the event is triggered. When the event handler is invoked, this data can be accessed via [[Event::data]].
$append boolean whether to append new event handler to the end of the existing handler list. If `false`, the new handler will be inserted at the beginning of the existing handler list.
    public static function on($class, $name, $handler, $data = null, $append = true)
    {
        $class = ltrim($class, '\\');
        if ($append || empty(self::$_events[$name][$class])) {
            self::$_events[$name][$class][] = [$handler, $data];
        } else {
            array_unshift(self::$_events[$name][$class], [$handler, $data]);
        }
    }

Usage Example

Пример #1
2
 public function init()
 {
     parent::init();
     if (is_null($this->attribute)) {
         throw new Exception("Module \"attribute\" attribute must be set");
     }
     if (is_null($this->latitudeAttribute)) {
         throw new Exception("Module \"latitudeAttribute\" attribute must be set");
     }
     if (is_null($this->longitudeAttribute)) {
         throw new Exception("Module \"longitudeAttribute\" attribute must be set");
     }
     if (is_null($this->jsonAttribute)) {
         throw new Exception("Module \"jsonAttribute\" attribute must be set");
     }
     if (is_null($this->class)) {
         $this->class = __NAMESPACE__ . '\\models\\Locations';
     }
     $location = new $this->class();
     $location->setAttributes(['destinationAttribute' => $this->attribute, 'latitudeAttribute' => $this->latitudeAttribute, 'longitudeAttribute' => $this->longitudeAttribute, 'jsonAttribute' => $this->jsonAttribute]);
     $this->location = $location;
     Event::on(Locations::className(), Locations::EVENT_ADD_LOCATION, [Locations::className(), 'addLocation']);
     Event::on(Locations::className(), Locations::EVENT_GET_LOCATION, [Locations::className(), 'getLocation']);
     return true;
 }
All Usage Examples Of yii\base\Event::on