Home  >  Article  >  php教程  >  Yii framework analysis (5) - Let’s talk about the CComponent basic class again

Yii framework analysis (5) - Let’s talk about the CComponent basic class again

黄舟
黄舟Original
2016-12-27 11:19:001164browse

This article can be used as a supplement to "Yii Framework Analysis (2) - CComponent Class Analysis".

The CComponent class provides the foundation for component-based and event-driven programming of the YII framework. Most classes in the YII framework use the CComponent class as a base class. The CComponent class provides 3 features for its subclasses:

1. Member variable expansion
Define a member variable by defining two member functions (getXXX/setXXX), such as:
public function getText() {…}
public function setText {…}
This is equivalent to defining a $text member variable, which can be called like this

$a=new CComponent;
$a=$component->text; // 等价于$a=$component->getText();
$component->text='abc'; // 等价于$component->setText('abc');

CComponent uses the magic method __get and __set to implement the "member variable expansion" feature. If a member variable that does not exist in the class itself is operated, PHP will call the __get and __set methods of this class for processing. CComponent uses these two magic methods to implement the "member variable expansion" feature. The following figure describes a subclass of CComponent, which adds two member variables, active and sessionName. The figure describes the calling process of these two member variables.

Yii framework analysis (5) - Let’s talk about the CComponent basic class again

(Note: Regarding the use of magic methods __get and __set, please refer to: PHP Basics and Objects 13 - Overloading)

In object-oriented programming, it is enough to directly define a member variable. Why does CComponent implement a member variable by defining two functions? One of the main reasons is the need to "delay loading" of member variables. Generally, the member variables of a class are uniformly assigned in the constructor or initialization function, but not every member variable will be assigned during the processing of a web request. For example, the App class defines two member variables: $cache and $db ($cache is a cache object, $db is a database link object). These two objects are created when the App class is initialized, but a web For some pages on the website, the content can be obtained through cache, so the database link object does not actually need to be created. If App is defined as a subclass of CComponent, two methods are defined in the App class: getCache/getDb, so that the getDb function is called to initialize the database link when the db member variable is used for the first time. Implement lazy loading - i.e. initialize on first use. Although delayed loading will add one function call, it can reduce unnecessary initialization of member variables (generally improving the access speed of the website), and can make our code easier to maintain and expand.
Delayed loading should be the most important use of the "member variable expansion" feature. Of course, this feature will have other uses. Think about it, when you operate a member variable, you are actually calling getXXX and setXXX Member function, you are calling a piece of code!

2. Event model
The event model is the "observer pattern" in the design pattern: when the state of an object changes, then this object can notify other objects of the event.
In order to use the event model, these three steps need to be implemented: 1. Define the event; 2. Register the event handler; 3. Trigger the event.
The subclass of CComponent defines an event by defining a member function starting with on, such as: public function onClick(){...}, and then registers the event handler by calling the attachEventHandler member function (multiple event handlers can be registered) , and finally trigger the event by calling raiseEvent.
The CComponent class uses a private member variable to save the event and all handles for processing the event. This member variable can be regarded as a hash table. The key of the hash table is the name of the event, and the value of the hash table is the event processing function linked list. .

Yii framework analysis (5) - Let’s talk about the CComponent basic class again

*#*3. Behavior class binding*#*There are two ways to add features to a class: 1. Directly modify the code of this class and add some member functions and member variables; 2. Derive and extend through subclasses. Obviously the second method is easier to maintain and expand. If multiple features need to be added to a class (multiple people at different times), then multi-level derivation is required, which obviously increases maintenance costs. *#*CComponent uses a special way to extend class information-behavior class binding. The behavior class is a subclass of CBehavior. CComponent can add one or more member functions and member variables of the CBehavior class to itself, and uninstall some CBehavior classes when not needed. The following is a simple example: *#*//Calculator class*#*class Calculator extends CBehavior*#*{*#*public function add($x, $y) { return $x + $y; }*# *public function sub($x, $y) { return $x - $y; }*#*...*#*}*#*$comp = new CComponent();*#*//For my class Add calculator function*#*$comp->attachbehavior('calculator', new Calculator());*#*$comp->add(2, 5);*#*$comp->sub(2 , 5);*#*CComponent implements "behavior class binding" through three magic methods: __get, __set and __call (for details on the use of these three magic methods, see: PHP Basic Classes and Objects 13 - Overloading) "Defined" feature, when calling member variables and member methods that do not exist in the CComponent class, the CComponent class will use these three magic methods to search on the "dynamically bound behavior object". That is, routing non-existent member variables and member methods to the "dynamic binding object". *#**#**#**#**#**#**#**#*You can use 3 sentences to summarize the characteristics of the CComponent class: *#*1. Better configure an object when setting When accessing the member variables of an object, it actually runs a piece of code; *#*2. Better monitoring of an object. When the internal state of the object changes, other objects can be notified; *#*3. Better expansion An object can add member variables and member functions to an object, and can also monitor the status of the object. *#**#* The above is Yii framework analysis (5) - let’s talk about the content of the CComponent basic class. For more related content, please pay attention to the PHP Chinese website (www.php.cn)! *#**#**#**#**#*
Statement:
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