亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
Detailed explanation of Yii framework components and event behavior management, yii behavior management
Home Backend Development PHP Tutorial Detailed explanation of Yii framework components and event behavior management, yii behavior management_PHP tutorial

Detailed explanation of Yii framework components and event behavior management, yii behavior management_PHP tutorial

Jul 12, 2016 am 08:50 AM
yii event components

Detailed explanation of Yii framework components and event behavior management, yii behavior management

This article describes Yii framework components and event behavior management with examples. Share it with everyone for your reference, the details are as follows:

Yii is a component-based, high-performance PHP framework for developing large-scale web applications. CComponent is almost the base class of all classes. It controls the management of components and events. Its methods and properties are as follows. The private variable $_e data stores events (evnet, called hook in some places), and the $_m array stores behavior (behavior).

Component Management

YII is a pure oop framework. Member variables in many classes are protected or private. CComponent uses the magic methods __get() and __set() in PHP to access and set properties, but the role of these methods Far from that. Use __get() to illustrate below

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 the CComponent or its subclass object instance $obj->name, __get($name) method:

1. First determine whether there is a getName() method in the instance. If so, return . If not, perform step 2

2. Determine whether it starts with on. Events starting with on are generally reserved events in CComponent subclasses. They are used to hang events. Use method_exists($this,$name) to determine whether the name exists in a class. In the instance, if it exists, return the event, otherwise execute step 3

3. If name exists in the behavior array, return the changed behavior. If it does not exist, perform step 4

4. Traverse the behavior array. The behavior in the array is an instance of the CBehavior subclass, and CBehavior is a subclass of CComponent, so use a recursive method to get the method in the behavior. If not, perform step 5

5. Exception thrown: The requested attribute does not exist.

The __get() method can be overloaded in CComponent subclasses. For example, the judgment of obtaining components is added to CModule. This brings up a problem. It is best not to have the same name for attributes and components, because the program will load the component first, and the attributes we want may not be obtained. If the names must be the same, getters must be used to obtain the attributes.

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

Regarding the loading and creation of components, the last YII framework analysis note 1: There is a question in point 3 of the YII execution process: When registering the core components of the framework, so many are loaded at once. Does it affect performance? Actually no. When registering, you just save the component and its corresponding configuration in the form of key-value pairs in an array (except for preloaded ones). When it is used, you can create the component as above, which will be done through createComponent( in YIIBase ) method is created and initialized. When calling __get() or getComponent() through CModule or its descendants (such as CWebApplication) to obtain components, CModule establishes an object pool through the $_components array to ensure that each component is only instantiated once in a request.

Event Behavior Management

Events are equivalent to extensions or plug-ins to a component. The hooks reserved in the component are used to realize the internal call of the component and the external control of the component. In the CComponent subclass, you can define methods starting with on as events, similar to onclick, onchange, etc. in js. In fact, the principles are 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, when calling the run() method in CApplication, before processing the request, first determine whether the handle of the onBeginRequest event is passed externally. If so, call the raiseEvent() method in CComponent through the onBeginRequest($event) method to execute the function in the handle or method.

Behavior is an upgraded version of events, and all behaviors are subclasses of CBeavior. Analyzing step 4 of the above __get() method analysis, we can see that the role of behavior is to completely extend the characteristics of the component, which can be properties, methods, events and even behaviors, which makes program development more flexible.

Another function of behavior is to put similar event handles together. When the behavior executes the attach() method, it will bind the event handle returned in the events() method. This achieves the purpose of aspect management and expansion. For example, CModelBehavior collects model-related events to facilitate the reuse of its subclasses. We can inherit it when we need to add behaviors to the model.

PS:The editor here recommends a PHP formatting and beautifying typesetting tool on this website to help you code typesetting in future PHP programming:

PHP code online formatting and beautification tool: http://tools.jb51.net/code/phpformat

Readers who are interested in more Yii-related content can check out the special topics on this site: "Introduction to Yii Framework and Summary of Common Techniques", "Summary of Excellent PHP Development Framework", "Basic Tutorial for Getting Started with Smarty Templates", "php Date and Time" Usage Summary", "php object-oriented programming introductory tutorial", "php string (string) usage summary", "php mysql database operation introductory tutorial" and "php common database operation skills summary"

I hope this article will be helpful to everyone’s PHP program design based on the Yii framework.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1133126.htmlTechArticleDetailed explanation of Yii framework components and event behavior management, yii behavior management This article describes Yii framework components and event behavior management with examples. Share it with everyone for your reference, the details are as follows: Yii is a...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to install the Windows 10 old version component DirectPlay How to install the Windows 10 old version component DirectPlay Dec 28, 2023 pm 03:43 PM

Many users always encounter some problems when playing some games on win10, such as screen freezes and blurred screens. At this time, we can solve the problem by turning on the directplay function, and the operation method of the function is also Very simple. How to install directplay, the old component of win10 1. Enter "Control Panel" in the search box and open it 2. Select large icons as the viewing method 3. Find "Programs and Features" 4. Click on the left to enable or turn off win functions 5. Select the old version here Just check the box

How to open the settings of the old version of win10 components How to open the settings of the old version of win10 components Dec 22, 2023 am 08:45 AM

Win10 old version components need to be turned on by users themselves in the settings, because many components are usually closed by default. First we need to enter the settings. The operation is very simple. Just follow the steps below. Where are the win10 old version components? Open 1. Click Start, then click "Win System" 2. Click to enter the Control Panel 3. Then click the program below 4. Click "Enable or turn off Win functions" 5. Here you can choose what you want to open

Get upcoming calendar events on your iPhone lock screen Get upcoming calendar events on your iPhone lock screen Dec 01, 2023 pm 02:21 PM

On iPhones running iOS 16 or later, you can display upcoming calendar events directly on the lock screen. Read on to find out how it's done. Thanks to watch face complications, many Apple Watch users are used to being able to glance at their wrist to see the next upcoming calendar event. With the advent of iOS16 and lock screen widgets, you can view the same calendar event information directly on your iPhone without even unlocking the device. The Calendar Lock Screen widget comes in two flavors, allowing you to track the time of the next upcoming event, or use a larger widget that displays event names and their times. To start adding widgets, unlock your iPhone using Face ID or Touch ID, press and hold

Angular components and their display properties: understanding non-block default values Angular components and their display properties: understanding non-block default values Mar 15, 2024 pm 04:51 PM

The default display behavior for components in the Angular framework is not for block-level elements. This design choice promotes encapsulation of component styles and encourages developers to consciously define how each component is displayed. By explicitly setting the CSS property display, the display of Angular components can be fully controlled to achieve the desired layout and responsiveness.

How to build event-based applications using PHP How to build event-based applications using PHP May 04, 2024 pm 02:24 PM

Methods for building event-based applications in PHP include using the EventSourceAPI to create an event source and using the EventSource object to listen for events on the client side. Send events using Server Sent Events (SSE) and listen for events on the client side using an XMLHttpRequest object. A practical example is to use EventSource to update inventory counts in real time in an e-commerce website. This is achieved on the server side by randomly changing the inventory and sending updates, and the client listens for inventory updates through EventSource and displays them in real time.

How to implement change event binding of select elements in jQuery How to implement change event binding of select elements in jQuery Feb 23, 2024 pm 01:12 PM

jQuery is a popular JavaScript library that can be used to simplify DOM manipulation, event handling, animation effects, etc. In web development, we often encounter situations where we need to change event binding on select elements. This article will introduce how to use jQuery to bind select element change events, and provide specific code examples. First, we need to create a dropdown menu with options using labels:

What is the difference between php framework laravel and yii What is the difference between php framework laravel and yii Apr 30, 2025 pm 02:24 PM

The main differences between Laravel and Yii are design concepts, functional characteristics and usage scenarios. 1.Laravel focuses on the simplicity and pleasure of development, and provides rich functions such as EloquentORM and Artisan tools, suitable for rapid development and beginners. 2.Yii emphasizes performance and efficiency, is suitable for high-load applications, and provides efficient ActiveRecord and cache systems, but has a steep learning curve.

A Deep Dive into Close Button Events in jQuery A Deep Dive into Close Button Events in jQuery Feb 24, 2024 pm 05:09 PM

In-depth understanding of the close button event in jQuery During the front-end development process, we often encounter situations where we need to implement the close button function, such as closing pop-up windows, closing prompt boxes, etc. When using jQuery, a popular JavaScript library, it becomes extremely simple and convenient to implement the close button event. This article will delve into how to use jQuery to implement close button events, and provide specific code examples to help readers better understand and master this technology. First, we need to understand how to define

See all articles