Yii is a component-based (component-based) Web framework, and the Ccomponent class is the base class for all components.
The Ccomponent class provides an attribute-based (property), event, and behavior (behavior) programming interface for subclasses.
1. Properties of the component
The Ccomponent class does not provide a variable store of attributes, and it requires a subclass to provide two methods to implement it. The Getpropertyname () method of the subclass provides $component->propertyname value manipulation data, and the subclass's Setpropertyname ($val) method provides $component-> PropertyName assignment operation.
$width = $component->textwidth; Get TextWidth Property
Implemented by calling the method provided by the subclass $width = $component->gettextwidth ()
$component->textwidth= $width; Setting the TextWidth Property
Implemented by calling the method provided by the subclass $component->settextwidth ($width)
Public Function Gettextwidth () { return $this->_textwidth;} Public Function Settextwidth ($value) { $this->_textwidth= $value;}
The property value of the component is case insensitive (the members of the class are case-sensitive)
2. Component Events (event)
A component event is a special property that registers (binds) an event-handling handle (which can be a function name, a class method, or an object method) to an event name that is automatically invoked when the event is aroused.
Component events are stored in the ccomponent $_e[] array, the key value of the array is the name of the event, the value of the key is a CList object, CList is a queue container provided by Yii, and the CList method Add () adds the callback handle of the event.
Add a global function to event handling
$component, onbeginrequest= "Logrequest";
Adding a class static method to an event handler
$component-Onbeginrequest=array ("CLog", "logrequest");
Adding an object method to event handling
$component-Onbeginrequest=array ($mylog, "logrequest");
Evoke events:
$component->raiseevent (' OnBeginRequest ', $event);
is automatically called:
Logrequest ($event), Clog:: Logrequest ($event) and $mylog.logrequest ($event)
The event handle must be defined as follows:
function MethodName ($event)
{
......
}
$event parameter is an instance of CEvent or its subclasses, which contains at least the message "who hangs this event".
The name of the event begins with "On" and can be used to differentiate properties and events in __get () and __set ().
3. Component behavior (behavior)
The behavior of a component is a way to extend component functionality without inheritance (see policy patterns in design mode).
The behavior class must implement the Ibehavior interface, and most of the behavior can be extended from the Cbehavior base class.
The Ibehavior interface provides 4 methods.
Attach ($component) associates itself to a component, detach ($component) disassociate $component, getenabled (), and setenabled () set the validity of the behavior object.
The behavior object is stored in the $_m[] array of the component, the array key value is the behavior name string, and the array value is the behavior class object.
Components extend a behavior through Attachbehavior ($name, $behavior):
$component-Attachbehavior (' render ', $htmlRender)
Added a $component name to render, $htmlRender need to be an object that implements the Ibehavior interface, or an array:
Array (' class ' = ' Path.to.BehaviorClass ', ' property1′=> ' value1′, ' property2′=> ' value2′,*)
The behavior object is created based on the class of the array and the property values are set.
The $htmlRender is stored in the $_m[' render '].
When externally invoking a method that is not defined by a component, the Magic Method __call () iterates through all the behavior objects, and is called if a method of the same name is found.
For example $htmlRender there is a method Renderfromfile (), you can access it directly as a method of the component:
Renderfromfile, $component ()
4.CComponent Source Code Analysis
The base class of all the parts ccomponent{private $_e; Private $_m; Gets the part properties, events, and behavior of the Magic method public function __get ($name) {$getter = ' get '. $name; Whether the property has a Get method if (method_exists ($this, $getter)) return $this-$getter (); Starts with on, gets the event handle handle else if (strncasecmp ($name, ' on ', 2) ===0 && method_exists ($this, $name)) {/ /Event name Lowercase $name =strtolower ($name); If _e[$name] does not exist, returns an empty CList event handle to the queue object if (!isset ($this->_e[$name])) $this->_e[$name]=new C List; Returns the handle queue object stored in _e[$name] return $this->_e[$name]; }//_m[$name] where the behavior object is stored returns the else if (isset ($this->_m[$name])) return $this->_m[$name]; else throw new CException (Yii::t (' Yii ', ' property ' {class}.{ Property} "was not defined. ', Array (' {class} ' =>get_class ($this), ' {property} ' = $name))); }/** * PHP Magic Method * Set the properties and things of the componentPieces */Public function __set ($name, $value) {$setter = ' set '. $name; Whether there is a set method for the attribute if (method_exists ($this, $setter)) $this-$setter ($value); The name begins with on, which is the event handling handle else if (strncasecmp ($name, ' on ', 2) ===0 && method_exists ($this, $name)) { Event name Lowercase $name =strtolower ($name); _e[$name] does not exist create a CList object if (!isset ($this->_e[$name])) $this->_e[$name]=new CList; Add an event handling handle $this->_e[$name]->add ($value); }//property has no set method, only get method, is read-only property, throws exception else if (Method_exists ($this, ' get '. $name)) throw new CException ( Yii::t (' Yii ', ' property ' {class}.{ Property} "was read only.", Array (' {class} ' =>get_class ($this), ' {property} ' = ' = $name))); else throw new CException (Yii::t (' Yii ', ' property ' {class}.{ Property} "was not defined. ', Array (' {class} ' =>get_class ($this), ' {property} ' => $name))); }/** * PHP Magic Method * provides the isset () function with the possibility of the existence of a property and event handling handle (*/Public Function __isset ($name) {$gett Er= ' get '. $name; if (Method_exists ($this, $getter)) return $this, $getter ()!==null; else if (strncasecmp ($name, ' on ', 2) ===0 && method_exists ($this, $name)) {$name =strtolower ($name); return Isset ($this->_e[$name]) && $this->_e[$name]->getcount (); } else return false; }/** * PHP Magic Method * Set the property value to null or delete the handle handle corresponding to the event name */Public Function __unset ($name) {$setter = ' Set '. $name; if (Method_exists ($this, $setter)) $this, $setter (null); else if (strncasecmp ($name, ' on ', 2) ===0 && method_exists ($this, $name)) unset ($this->_e[strtolower ($nam e)]); else if (method_exists ($this, ' get ' $name)) throw new CException (Yii::t (' Yii ', ' property ' {class}.{ Property} "was read only. ',Array (' {class} ' =>get_class ($this), ' {property} ' = $name))); /** * PHP Magic Method * ccomponent undefined class method, look for the same name method in the behavior class, implement the behavior method of call */Public Function __call ($name, $pa Rameters) {//The $_m array stored in the behavior class is not empty if ($this->_m!==null) {//Loops out $_m array of behavior Class F Oreach ($this->_m as $object) {//The Behavior class object is valid, and the method exists, called if ($object->enabled &A mp;& method_exists ($object, $name)) return Call_user_func_array (Array ($object, $name), $parameters); }} throw new CException (Yii::t (' Yii ', ' {class} does not has a method named ' {name} '. ', Array (' {class} ' =>get_class ($this), ' {name} ' = = $name))); }/** * Returns the behavior class object based on the behavior name */Public Function ASA ($behavior) {return isset ($this->_m[$behavior])? $th is->_m[$behavior]: null; }/** * Attaches a list of behaviors to the component. * Each behavior are indexed by its nameAnd should is an instance of * {@link ibehavior}, a string specifying the behavior class, or an * array of The following structure: * <pre> * Array (* ' class ' = ' Path.to.BehaviorClass ', * ' property1′=> ' value1′, * ' property2′=> ' value2′, *) * & lt;/pre> * @param array List of behaviors to being attached to the component * @since 1.0.2 */Public function Attachbehaviors ($behaviors) {//$behaviors an array $name = = $behavior foreach ($behaviors as $name + = $beha Vior) $this->attachbehavior ($name, $behavior); }/** * Add a behavior to component */Public function Attachbehavior ($name, $behavior) {/* $behavior not an instance of the Ibehavior interface. is * Array (* ' class ' = ' Path.to.BehaviorClass ', * ' Property1′=> ' value1′, * ' property2′=> ' value2′, *) * passed to yii::createcomponent create behavior and Initialize object properties */if (!) $behavior instanceof Ibehavior)) $behavior =yii::createcomponent ($behavior); $behavior->setenabled (TRUE); $behavior->attach ($this); return $this->_m[$name]= $behavior; }/** * raises an event. * This method represents the happening of an event. It invokes * All attached handlers for the event. * @param string The event name * @param CEvent the event parameter * @throws CException if the event is undefined or An event handler is invalid. */Public Function RaiseEvent ($name, $event) {$name =strtolower ($name); _e[$name] The event handler queue exists if (Isset ($this->_e[$name])) {//loop out event handling handle foreach ($this-&G t;_e[$name] as $handler) {//Event handling handle is global function if (is_string ($handler)) Call_user_func ($handler, $event); else if (is_callable ($handler, tRue) {//an array:0–object, 1–method Name list ($object, $method) =$ Handler if (is_string ($object)) //static class method Call_user_func ($handler, $event); else if (method_exists ($object, $method)) $object-$method ($event); else throw new CException (Yii::t (' Yii ', ' Event ' {class}.{ Event} "is attached with an invalid handler" {handler} ". ', Array (' {class} ' =>get_class ($this), ' {event} ' = = $name, ' { handler} ' = $handler [1])); } else throw new CException (Yii::t (' Yii ', ' Event ' {class}.{ Event} "is attached with an invalid handler" {handler} ". ', Array (' {class} ' =>get_class ($this), ' {event} ' = = $name, ' { Handler} ' =>gettype ($handler))); $event handled is set to true after stopping the call of the remaining handle in the queue if (($event instanceof CEvent) && $event->handled) RetUrn }} else if (Yii_debug &&! $this->hasevent ($name)) throw new CException (' YII ', ' Event ' {class }. {event} ' is not defined. ', Array (' {class} ' =>get_class ($this), ' {event} ' = = $name))); }}
Yii Framework Analysis (ii)--ccomponent class anatomy