Parsing of YII framework components and event behavior management

Source: Internet
Author: User
Tags php framework yii
This article mainly introduces the YII framework component and event behavior management, analyzes the principle and use skill of YII framework component management and behavior management in detail, and needs friends to refer to the following

The examples in this paper describe the YII framework components and event behavior management. Share to everyone for your reference, 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, which controls the management of components and events, with methods and properties such as private variables $_e data storage events (Evnet, some places called hooks), $_m array storage behavior (behavior).

Component Management

Yii is a purely OOP framework, where member variables in many classes are protected or private, and Ccomponent uses the Magic Method __get (), __set () in PHP to access and set properties, but these methods do not refer to them much. 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 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} "was not defined. ',    Array (' {class} ' =>get_class ($this), ' {property} ' = $name)));}

When Ccomponent or its subclass object instance $obj->name, the __get ($name) method:

1, first determine whether there is a getname () method in the instance, if there is a return if there is no 2nd step

2, judge whether to start with on, the general is the Ccomponent sub-class reserved events, with hanging in the event, through Method_exists ($this, $name) to determine whether the name exists in the instance of the class, if present, return event, Otherwise, take step 3rd.

3, if the name exists in the behavior array, returns the change behavior, if not present, executes the 4th step

4, the traversal behavior array, the array behavior is the Cbehavior subclass the instance, but Cbehavior is also the Ccomponent neutron class, therefore uses the recursive method to obtain the method in the behavior, if has not, carries on the 5th step

5. Throw exception: The requested property does not exist.

The __get () method can be overloaded in the Ccomponent subclass, such as the decision to add a get component in Cmodule. It is important to note that the attribute and component names are best not to have the same name, because the program will load the component first, it may not get the attributes we want, and if it has to be the same, get the property with a getter.

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 3rd question in the implementation process of YII framework Analysis Note 1:YII is: Does it affect performance when it loads so much at the time of registering the core components of the framework? In fact, when registering a component and its corresponding configuration with key-value pairs in the form of the array (except preloaded), when used to create the component as above, will be created by the Createcomponent () method in Yiibase, and initialized. When you call __get () or getcomponent () from Cmodule or its descendant class (such as Cwebapplication) to get the component, Cmodule creates an object pool from the $_components array, ensuring that each component is instantiated only once in a single request.

Event Behavior Management

An event is an extension to a component or a plug-in that implements a component's internal call to an external, external, component control with hooks reserved in the component. In the Ccomponent subclass can be defined in the beginning of the method as the event, similar to JS in the onclick, onchange, etc., in fact, the principle is similar. All events are subclasses of CEvent in the same file as Ccomponent.

/*** 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 is loads static application components. Derived classes usually overrides this* method to does more application-specific tasks.* Remember to call the parent Impleme Ntation So, static application components is 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 external descendant is a handle to the OnBeginRequest event before processing the request, and if so, through OnBeginRequest ($event) The method calls the RaiseEvent () method in Ccomponent to execute the function or method in the handle.

Behavior is an upgraded version of an event, and all behaviors are cbehavior subclasses. Analysis of the __get () method in the above analysis of the 4th step can be seen that the role of the behavior is to fully extend the characteristics of the component, can be properties, methods, events and even behavior, which makes the development of the program more flexible.

Another function of behavior is to put similar event handles together and bind the event handle returned in the events () method when the behavior executes the attach () method, which is done to achieve the purpose of aspect management and extension. For example, cmodelbehavior the model-related events to facilitate the reuse of their subclasses, which can be inherited when we need to add behavior to the model.

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.