Yii Framework components and event behavior management details, yii behavior management _ PHP Tutorial

Source: Internet
Author: User
Detailed description of Yii Framework components and event behavior management, and yii behavior management. Yii Framework components and event behavior management details. yii behavior management this article describes the Yii Framework components and event behavior management. Yii is a Yii Framework component and event behavior management module, and yii behavior management module for your reference.

This article describes the Yii Framework components and event behavior management. We will share this with you for your reference. The details are as follows:

Yii is a high-performance PHP framework based on components for developing large Web applications. CComponent is the base class of almost all classes. it controls the management of components and events. its methods and attributes are as follows: private variable $ _ e data storage event (evnet, in some cases, it is called hook) and the $ _ m array storage behavior (behavior ).

Component Management

YII is a pure oop framework. member variables in many classes are protected or private. in CComponent, use the magic method _ get () ,__ set () in php () to access and set properties, but these methods do not. The following uses _ get () to describe

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}" is not defined.',    array('{class}'=>get_class($this), '{property}'=>$name)));}

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

1. First, determine whether the getName () method exists in the instance. If yes, return. If no step 2nd is executed

2. determine whether it starts with "on". generally, events starting with "on" are reserved in the CComponent Subclass. use and attach events through method_exists ($ this, $ name) checks whether the name exists in the instance of the class. If yes, the system returns an event. Otherwise, the system executes step 1.

3. if the name exists in the behavior array, the system returns the change behavior. if the name does not exist, perform step 1.

4. traverse the behavior array. the behavior in the array is the instance of the CBehavior subclass, and the CBehavior is the subclass of CComponent. Therefore, use recursive methods to obtain the method in the behavior. if not, perform step 1.

5. throw an exception: The request property does not exist.

You can overload the _ get () method in the CComponent Subclass. for example, you can add the judgment to the CModule to obtain the component. Note that the attribute and component name should not be duplicated, because the program will give priority to loading the component and may not obtain the desired attribute. if the attribute must be duplicated, you must use getter to obtain the attributes.

public function __get($name){  if($this->hasComponent($name))    return $this->getComponent($name);  else    return parent::__get($name);}

About the component loading and creation, I have a question in the previous YII Framework Analysis Note 1: Point 3rd in the YII execution process: when I register the core components of the framework, I suddenly load so many components. does this affect the performance? Actually, no. during registration, only the components and their corresponding configurations are saved in the array in the form of key-value pairs (except for pre-loading ), the component will be created and initialized by using the createComponent () method in YIIBase. When _ get () or getComponent () is called by CModule or its child classes (such as CWebApplication) to obtain a component, CModule creates an object pool through the $ _ components array, make sure that each component is instantiated only once in one request.

Event behavior management

An event is equivalent to an extension or plug-in for a component. the hooks reserved in the component are used to implement internal call of the component and external control of the component. In the CComponent subclass, you can define methods starting with on as events, similar to onclick and onchange in js. 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 loads static application components. Derived classes usually overrides this* method to do more application-specific tasks.* Remember to call the parent implementation so that 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, in CApplication, call the run () method to determine whether to pass the onBeginRequest event handle externally before processing the request. If yes, use the onBeginRequest ($ event) method to call raiseEvent () in CComponent () method.

Action is an upgraded version of the event, and all actions are subclasses of CBehavior. Analyze the above step 1 of The _ get () method analysis, we can see that the role of behavior is to fully expand the features of the component, which can be attributes, methods, events, and even behaviors, this makes program development more flexible.

Another function of behavior is to put the similar event handle together. when the attach () method is executed, the event handle returned by the events () method is bound, in this way, management and expansion are achieved. For example, CModelBehavior aggregates model-related events to facilitate the reuse of child classes. when we need to add behavior for the model, we can inherit it.

PS: I recommend a php formatting and formatting typographical tool on this site to help you typeset code in future PHP programming:

Php code online formatting and beautification tools:Http://tools.jb51.net/code/phpformat

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.