This article describes the Yii framework component and event behavior management. Share to everyone for your reference, specific as follows:
Yii is a component-based, high-performance PHP Framework for developing large WEB applications. Ccomponent is almost the base class for all classes, it controls the management of components and events, its methods and properties are as follows, private variables $_e data storage events (Evnet, some places are called hooks), $_m array storage behavior (behavior).
Component Management
Yii is a pure OOP framework, the member variables in many classes are protected or private, ccomponent use the Magic method in PHP __get (), __set () to access and set properties, but these methods do not refer to the role of these. Use __get () below to illustrate
Public Function __get ($name)
{
$getter = ' get '. $name;
if (Method_exists ($this, $getter)) return
$this-> $getter ();
else if (strncasecmp ($name, ' on ', 2) ===0 && method_exists ($this, $name))
{
//duplicating Geteventhandlers () Here is for performance
$name =strtolower ($name);
if (!isset ($this->_e[$name])
$this->_e[$name]=new CList;
return $this->_e[$name];
}
else if (isset ($this->_m[$name)) return
$this->_m[$name];
else if (Is_array ($this->_m))
{
foreach ($this->_m as $object)
{
if ($object-> Getenabled () && (Property_exists ($object, $name) | | | $object->cangetproperty ($name)) return
$object- > $name;
}
}
throw new CException (Yii::t (' Yii ', ' property ') {class}.{ Property} ' are not defined. ',
Array (' {class} ' =>get_class ($this), ' {property} ' => $name));
}
When Ccomponent or its subclass object instance $obj->name, the __get ($name) method:
1, first of all, to determine whether there is a getname () method, if there is returned, if the 2nd step is not performed
2, to determine whether it is starting with on, usually on the beginning of the Ccomponent subclass of the event reserved, with and hanging in the event, through the Method_exists ($this, $name) to determine whether the name exists in the class of instances, if there is, return event, Otherwise perform step 3rd
3, if the name exists in the behavior array, return the change behavior, if not present, perform the 4th step
4, the traversal behavior array, the array behavior is an instance of Cbehavior subclass, and Cbehavior is Ccomponent neutron class, so use recursive method to obtain the method in the behavior, if not, perform the 5th step
5, throw exception: The requested property does not exist.
You can overload the __get () method in a ccomponent subclass, such as adding a decision to get a component in Cmodule. This is a problem. Attribute and component names are best not to duplicate the name, because the program will load the component first, it may not get the attribute we want, if it must be duplicate, we need to use getter to get the attribute.
Public Function __get ($name)
{
if ($this->hascomponent ($name)) return
$this->getcomponent ($name) ;
else return
Parent::__get ($name);
On the loading and creation of components, the last article in the YII Framework Analysis Note 1:yii the 3rd in the implementation process is a question: the core components of the registration framework when loading so much, is not affecting performance? In fact, when registering, you simply save the component and its corresponding configuration in the array in the form of a key-value pair (except preload), and when you use it to create the component, it is created and initialized by the Createcomponent () method in Yiibase. When you invoke __get () or getcomponent () to get a component by Cmodule or its descendants (such as Cwebapplication), Cmodule establishes the object pool through the $_components array, ensuring that each component is instantiated only once in one request.
Event Behavior Management
An event is equivalent to an extension or plug-in of a component, which is partially controlled externally and externally to the component's internal call with the hooks reserved in the component. In the Ccomponent subclass, you can define a method that starts with on as an event, similar to the onclick, onchange, and so on in JS, but the principle is almost the same. All events are subclasses that are cevent with ccomponent in the same file.
/**
* Raised right before the application processes the request.
* @param CEvent $event the event parameter
* * Public
function OnBeginRequest ($event)
{
$this-> RaiseEvent (' OnBeginRequest ', $event);
}
/**
* Runs the application.
* This method loads static application components. Derived classes usually overrides this * method to does more
application-specific tasks.
* Remember to call the parent implementation so-static application components are loaded.
*/Public
function run ()
{
if ($this->haseventhandler (' OnBeginRequest '))
$this-> OnBeginRequest (New CEvent ($this));
$this->processrequest ();
if ($this->haseventhandler (' onendrequest '))
$this->onendrequest (New CEvent ($this));
}
For example, call the Run () method in CApplication to determine whether the handle of an external OnBeginRequest event is passed before the request is processed, and if so, through OnBeginRequest ($event) Method calls the RaiseEvent () method in Ccomponent to execute a function or method in the handle.
Behavior is an upgraded version of an event, and all behaviors are cbehavior subclasses. Analysis of the above __get () method Analysis of the 4th step can be seen that the role of behavior is to fully extend the characteristics of the component, can be properties, methods, events and even behavior, so that the program development more flexible.
Another effect of behavior is to put together similar event handles that bind the event handles returned in the events () method when the behavior executes the attach () method, to achieve the purpose of management and extension. For example, cmodelbehavior the model-related events together to facilitate the reuse of its subclasses, which can be inherited when we need to add behavior to model.
PS: Small knitting here recommend a site for the layout of the PHP format landscaping tools to help you in the future of PHP programming code layout:
PHP Code online Format Landscaping tool:Http://tools.jb51.net/code/phpformat
More about Yii related content readers can view the site topics: "Yii framework Introduction and common skills Summary", "PHP Excellent Development Framework Summary", "Smarty Template Primer Tutorial", "PHP date and Time usage summary", "PHP object-oriented Programming Program", " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips
I hope this article will help you with the PHP program design based on the YII framework.