Class yii\base\Event
Event 是所有事件类的基类。
它封装了与事件关联的参数。
$sender 属性描述了谁触发了该事件。
并且 $handled 属性指示是否处理了事件。
如果事件处理程序将 $handled 设置为 true
,
其余的未经处理的处理程序将不再被调用来处理该事件。
此外,在附加事件处理程序时,可以传递额外的数据, 并在调用事件处理程序时通过 $data 属性使用。
有关 Event 的更多详细信息和使用信息,请参阅 事件的指南文章。
公共属性
属性 | 类型 | 描述 | 被定义在 |
---|---|---|---|
$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 |
公共方法
方法 | 描述 | 被定义在 |
---|---|---|
__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() | 从类级事件中卸载事件处理程序。 | yii\base\Event |
offAll() | 卸载所有已注册的类级事件处理程序。 | yii\base\Event |
on() | Attaches an event handler to a class-level event. | yii\base\Event |
trigger() | 触发类级事件。 This method will cause invocation of event handlers that are attached to the named event for the specified class and all its parent classes. | yii\base\Event |
属性详情
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.
方法详情
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. |
---|
从类级事件中卸载事件处理程序。
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.
参见 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().
参见 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 |
触发类级事件。 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. |