1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\base; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Component is the base class that implements the *property*, *event* and *behavior* features. |
14
|
|
|
* |
15
|
|
|
* Component provides the *event* and *behavior* features, in addition to the *property* feature which is implemented in |
16
|
|
|
* its parent class [[\yii\base\Object|Object]]. |
17
|
|
|
* |
18
|
|
|
* Event is a way to "inject" custom code into existing code at certain places. For example, a comment object can trigger |
19
|
|
|
* an "add" event when the user adds a comment. We can write custom code and attach it to this event so that when the event |
20
|
|
|
* is triggered (i.e. comment will be added), our custom code will be executed. |
21
|
|
|
* |
22
|
|
|
* An event is identified by a name that should be unique within the class it is defined at. Event names are *case-sensitive*. |
23
|
|
|
* |
24
|
|
|
* One or multiple PHP callbacks, called *event handlers*, can be attached to an event. You can call [[trigger()]] to |
25
|
|
|
* raise an event. When an event is raised, the event handlers will be invoked automatically in the order they were |
26
|
|
|
* attached. |
27
|
|
|
* |
28
|
|
|
* To attach an event handler to an event, call [[on()]]: |
29
|
|
|
* |
30
|
|
|
* ```php |
31
|
|
|
* $post->on('update', function ($event) { |
32
|
|
|
* // send email notification |
33
|
|
|
* }); |
34
|
|
|
* ``` |
35
|
|
|
* |
36
|
|
|
* In the above, an anonymous function is attached to the "update" event of the post. You may attach |
37
|
|
|
* the following types of event handlers: |
38
|
|
|
* |
39
|
|
|
* - anonymous function: `function ($event) { ... }` |
40
|
|
|
* - object method: `[$object, 'handleAdd']` |
41
|
|
|
* - static class method: `['Page', 'handleAdd']` |
42
|
|
|
* - global function: `'handleAdd'` |
43
|
|
|
* |
44
|
|
|
* The signature of an event handler should be like the following: |
45
|
|
|
* |
46
|
|
|
* ```php |
47
|
|
|
* function foo($event) |
48
|
|
|
* ``` |
49
|
|
|
* |
50
|
|
|
* where `$event` is an [[Event]] object which includes parameters associated with the event. |
51
|
|
|
* |
52
|
|
|
* You can also attach a handler to an event when configuring a component with a configuration array. |
53
|
|
|
* The syntax is like the following: |
54
|
|
|
* |
55
|
|
|
* ```php |
56
|
|
|
* [ |
57
|
|
|
* 'on add' => function ($event) { ... } |
58
|
|
|
* ] |
59
|
|
|
* ``` |
60
|
|
|
* |
61
|
|
|
* where `on add` stands for attaching an event to the `add` event. |
62
|
|
|
* |
63
|
|
|
* Sometimes, you may want to associate extra data with an event handler when you attach it to an event |
64
|
|
|
* and then access it when the handler is invoked. You may do so by |
65
|
|
|
* |
66
|
|
|
* ```php |
67
|
|
|
* $post->on('update', function ($event) { |
68
|
|
|
* // the data can be accessed via $event->data |
69
|
|
|
* }, $data); |
70
|
|
|
* ``` |
71
|
|
|
* |
72
|
|
|
* A behavior is an instance of [[Behavior]] or its child class. A component can be attached with one or multiple |
73
|
|
|
* behaviors. When a behavior is attached to a component, its public properties and methods can be accessed via the |
74
|
|
|
* component directly, as if the component owns those properties and methods. |
75
|
|
|
* |
76
|
|
|
* To attach a behavior to a component, declare it in [[behaviors()]], or explicitly call [[attachBehavior]]. Behaviors |
77
|
|
|
* declared in [[behaviors()]] are automatically attached to the corresponding component. |
78
|
|
|
* |
79
|
|
|
* One can also attach a behavior to a component when configuring it with a configuration array. The syntax is like the |
80
|
|
|
* following: |
81
|
|
|
* |
82
|
|
|
* ```php |
83
|
|
|
* [ |
84
|
|
|
* 'as tree' => [ |
85
|
|
|
* 'class' => 'Tree', |
86
|
|
|
* ], |
87
|
|
|
* ] |
88
|
|
|
* ``` |
89
|
|
|
* |
90
|
|
|
* where `as tree` stands for attaching a behavior named `tree`, and the array will be passed to [[\Yii::createObject()]] |
91
|
|
|
* to create the behavior object. |
92
|
|
|
* |
93
|
|
|
* For more details and usage information on Component, see the [guide article on components](guide:concept-components). |
94
|
|
|
* |
95
|
|
|
* @property Behavior[] $behaviors List of behaviors attached to this component. This property is read-only. |
96
|
|
|
* |
97
|
|
|
* @author Qiang Xue <[email protected]> |
98
|
|
|
* @since 2.0 |
99
|
|
|
*/ |
100
|
|
|
class Component extends Object implements ComponentInterface |
101
|
|
|
{ |
102
|
|
|
use ComponentTrait; |
103
|
|
|
} |
104
|
|
|
|