Class yii\base\Event
Inheritance | yii\base\Event » yii\base\BaseObject |
---|---|
Implements | yii\base\Configurable |
Subclasses | yii\base\ActionEvent, yii\base\ModelEvent, yii\base\ViewEvent, yii\base\WidgetEvent, yii\db\AfterSaveEvent, yii\httpclient\RequestEvent, yii\i18n\MissingTranslationEvent, yii\mail\MailEvent, yii\web\UserEvent |
Available since version | 2.0 |
Source Code | https://github.com/yiisoft/yii2/blob/master/framework/base/Event.php |
Event is the base class for all event classes.
It encapsulates the parameters associated with an event.
The $sender property describes who raises the event.
And the $handled property indicates if the event is handled.
If an event handler sets $handled to be true
, the rest of the
uninvoked handlers will no longer be called to handle the event.
Additionally, when attaching an event handler, extra data may be passed and be available via the $data property when the event handler is invoked.
For more details and usage information on Event, see the guide article on events.
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$data | mixed | The data that is passed to yii\base\Component::on() when attaching an event handler. | yii\base\Event |
$handled | boolean | Whether the event is handled. | yii\base\Event |
$name | string | The event name. | yii\base\Event |
$sender | object | The sender of this event. | yii\base\Event |
Public Methods
Method | Description | Defined By |
---|---|---|
__call() | Calls the named method which is not a class method. | yii\base\BaseObject |
__construct() | Constructor. | yii\base\BaseObject |
__get() | Returns the value of an object property. | yii\base\BaseObject |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\base\BaseObject |
__set() | Sets value of an object property. | yii\base\BaseObject |
__unset() | Sets an object property to null. | yii\base\BaseObject |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\base\BaseObject |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\BaseObject |
className() | Returns the fully qualified name of this class. | yii\base\BaseObject |
hasHandlers() | Returns a value indicating whether there is any handler attached to the specified class-level event. | yii\base\Event |
hasMethod() | Returns a value indicating whether a method is defined. | yii\base\BaseObject |
hasProperty() | Returns a value indicating whether a property is defined. | yii\base\BaseObject |
init() | Initializes the object. | yii\base\BaseObject |
off() | Detaches an event handler from a class-level event. | yii\base\Event |
offAll() | Detaches all registered class-level event handlers. | yii\base\Event |
on() | Attaches an event handler to a class-level event. | yii\base\Event |
trigger() | Triggers a class-level event. | yii\base\Event |
Property Details
The data that is passed to yii\base\Component::on() when attaching an event handler. Note that this varies according to which event handler is currently executing.
Whether the event is handled. Defaults to false
.
When a handler sets this to be true
, the event processing will stop and
ignore the rest of the uninvoked event handlers.
The event name. This property is set by yii\base\Component::trigger() and trigger(). Event handlers may use this property to check what event it is handling.
The sender of this event. If not set, this property will be
set as the object whose trigger()
method is called.
This property may also be a null
when this event is a
class-level event which is triggered in a static context.
Method Details
Returns a value indicating whether there is any handler attached to the specified class-level event.
Note that this method will also check all parent classes to see if there is any handler attached to the named event.
public static boolean hasHandlers ( $class, $name ) | ||
$class | string|object | The object or the fully qualified class name specifying the class-level event. |
$name | string | The event name. |
return | boolean | Whether there is any handler attached to the event. |
---|
Detaches an event handler from a class-level event.
This method is the opposite of on().
Note: in case wildcard pattern is passed for class name or event name, only the handlers registered with this wildcard will be removed, while handlers registered with plain names matching this wildcard will remain.
See also on().
public static boolean off ( $class, $name, $handler = null ) | ||
$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 |
return | boolean | Whether a handler is found and detached. |
---|
public static void offAll ( ) |
Attaches an event handler to a class-level event.
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:
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.
Since 2.0.14 you can specify either class name or event name as a wildcard pattern:
Event::on('app\models\db\*', '*Insert', function ($event) {
Yii::trace(get_class($event->sender) . ' is inserted.');
});
For more details about how to declare an event handler, please refer to yii\base\Component::on().
See also off().
public static void on ( $class, $name, $handler, $data = null, $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 yii\base\Event::$data. |
$append | boolean | Whether to append new event handler to the end of the existing
handler list. If |
Triggers a class-level event.
This method will cause invocation of event handlers that are attached to the named event for the specified class and all its parent classes.
public static void trigger ( $class, $name, $event = null ) | ||
$class | string|object | The object or the fully qualified class name specifying the class-level event. |
$name | string | The event name. |
$event | yii\base\Event | The event parameter. If not set, a default yii\base\Event object will be created. |