yii\base\Component::on PHP Method

on() public method

The event handler must be a valid PHP callback. The following are some examples: function ($event) { ... } // anonymous function [$object, 'handleClick'] // $object->handleClick() ['Page', 'handleClick'] // Page::handleClick() 'handleClick' // global function handleClick() The event handler must be defined with the following signature, function ($event) where $event is an Event object which includes parameters associated with the event.
See also: off()
public on ( string $name, callable $handler, mixed $data = null, boolean $append = true )
$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 function on($name, $handler, $data = null, $append = true)
    {
        $this->ensureBehaviors();
        if ($append || empty($this->_events[$name])) {
            $this->_events[$name][] = [$handler, $data];
        } else {
            array_unshift($this->_events[$name], [$handler, $data]);
        }
    }

Usage Example

Example #1
0
 public function on($name, $handler, $data = null, $append = true)
 {
     $internalHandler = static::INTERNAL_HANDLER . $this->_counter++;
     $this->_handlers[$name][$internalHandler] = $handler;
     parent::on($name, [$this, $internalHandler], $data, $append);
 }