Yii applications are built on components. A component is an instance of Ccomponent or its subclasses. The use of a component primarily involves accessing its properties and the time it is triggered or processed. The base class Ccomponent specifies how properties and events are defined.
1. Component Properties
The properties of a component are like the public member variables of an object. It is readable and writable. For example:
$width = $component->textwidth; Gets the TextWidth property $component->enablecaching=true; Setting the EnableCaching Property
To define a component property, we simply define a public member variable in the component class. A more flexible approach is to define its getter and setter methods, for example:
Public Function Gettextwidth () { return $this->_textwidth;} Public Function Settextwidth ($value) { $this->_textwidth= $value;}
The code above defines a writable attribute named textWidth
(the name is case insensitive). When the property is read, it is getTextWidth()
called, and its return value becomes the property value, similar to when the property is written setTextWidth()
. If the setter method is not defined, the property will be read-only and an exception will be thrown if written to it. There is an advantage to defining a property using getter and Setter methods: When you read or write an attribute, you can perform additional logic (for example, perform validation, trigger an event).
Note: There is a slight difference between a property defined by Getter/setter and a class member variable. The former name is case insensitive, while the latter is case-sensitive.
2. Component Events
Component events are special properties that use some 事件句柄 (event handlers)
of the called methods as their values. attaching (assigning) a method to an event will cause the method to be called automatically at the point where the event is aroused. As a result, the behavior of a component may be modified in a way that is not foreseen in the development process of the part.
component Events on
are defined at the beginning of the naming method . The name of the event is case insensitive, just as the attribute is defined by the Getter/setter method. The following code defines an onClicked
event:
Public Function onclicked ($event) { $this->raiseevent (' onclicked ', $event);}
Here, as an event argument, $event
is an instance of CEvent or its subclasses.
We can attach a method to this event, as follows:
$component->onclicked= $callback;
This $callback
points to a valid PHP callback. It can be a global function, or it can be a method in a class. If it is the latter, it must be provided in an array of ways: array($object,'methodName')
.
The structure of the event handle is as follows:
function MethodName ($event) { ...}
Here is the $event
argument that describes the event (it originates from the raiseEvent()
call). The $event
argument is an instance of CEvent or its subclasses. At a minimum, it contains information about who triggered this event.
Starting with version 1.0.10, the event handle can also be an anonymous function that is supported after PHP 5.3. For example
$component->onclicked=function ($event) { ...}
If we call now onClicked()
, the onClicked
event will be triggered (in onClicked()
), and the attached event handle will be automatically called.
An event can bind multiple handles. When an event is triggered, these handles are executed sequentially in the order in which they are bound to the event. If the handle determines that the subsequent handle of the organization is executed, it can be set $event->handled to True.
3. Component behavior
Starting with version 1.0.2, the component has added support for mixin and can bind one or more behaviors. A behavior is an object whose method can be implemented by a collection of functions by its bound parts 继承(inherited)
, rather than by proprietary inheritance (that is, ordinary class inheritance). A component can implement multiple behavior bindings in a ' multiple inheritance ' manner.
The behavior class must implement the Ibehavior interface . Most behaviors can inherit from Cbehavior. If a behavior needs to be bound to a model, it can also inherit from Cmodelbehavior or Cactiverecordbehavior, which implements the binding attribute specifically for the model.
to use a behavior, it must first be bound to a component by calling the Attach () method of this behavior . Then we can call this behavior method from the component:
$NAME implements the unique identification $component->attachbehavior ($name, $behavior) of the behavior in the component, and/or test () is the method in the behavior. $component->test ();
The bound behavior can be accessed just like a normal property in a component. For example, if a named tree
behavior is bound to a component, we can get a reference to this behavior by using the following code.
$behavior = $component->tree;//equals the downstream code://$behavior = $component->asa (' tree ');
The behavior can be temporarily banned, at which point its method is invalidated in the component. For example:
$component->disablebehavior ($name);//The following code throws an exception $component->test (); $component->enablebehavior ($name) ;//You can now use the $component->test ();
It is possible to bind two identically named behaviors to the same component . In this case, the first bound behavior has precedence.
When used in conjunction with events, the behavior is more powerful. When the behavior is bound to a component, some of the methods in the behavior can be bound to some events of the component. In this way, the behavior can observe or change the general execution process of the component.
Starting with version 1.1.0, a property of a behavior can also be accessed by the component to which it is bound. These properties contain public member variables and properties that are set by getters and/or setters methods. For example, if a behavior has a property of XYZ, this behavior is bound to the component $a, and then we can use an expression to $a->xyz
access the properties of this behavior.
The above is the Yii Framework Official Guide Series 10--Basics: Components of the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!