This article can be used as a supplement to the analysis of Yii Framework (ii)--ccomponent category.
The Ccomponent class provides the basis for component-based and event-driven programming of the YII framework, and most classes in the YII framework use the Ccomponent class as the base class. The Ccomponent class provides 3 properties for its subclasses:
1. Member Variable extension
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
$a =new ccomponent;
$a = $component->text; Equivalent to $a= $component->gettext ();
$component->text= ' abc '; Equivalent to $component->settext (' abc ');
Ccomponent is implemented by the Magic method __get and __set to implement the "Member Variable extension" feature, if the class itself does not exist in the member variables to operate, PHP will call this class of __get and __set methods to handle. Ccomponent uses these two magic methods to implement the "Member Variable Extension" feature. Describes a ccomponent subclass that adds active and sessionname two member variables that describe the invocation process for these two member variables.
(Note: For the use of magic methods __get and __set, refer to: Php Basics and Objects 13--overloading)
It is possible to define a member variable directly in object-oriented programming, why ccomponent to implement a member variable by defining 2 functions? One of the main reasons is that member variables need to be "deferred loading", generally, the member variables of a class are uniformly assigned in constructors or initialization functions, but not every member variable will be used during the processing of a Web request, such as two member variables defined in the App class: $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 some pages of a Web site can be retrieved from the cache, so the database link object does not need to be created. If the app is defined as a subclass of Ccomponent, define two methods in the App class: Getcache/getdb so that you can use the DB member variable for the first time, and then call the GETDB function to initialize the database link, so that the delay load is implemented-- That is, it is initialized on the first use. Although the deferred load increases the function call one time, it can reduce the initialization of unnecessary member variables (which in fact improves the website's access speed) and makes our code more maintainable and extensible.
Delay loading should be the most important use of the "Member Variable Extension" feature, of course, this feature will have other uses, think, when you manipulate a member variable, you are actually calling the GetXXX and SETXXX member functions, you are calling a piece of code!
2. Event Model
The event model is the "observer pattern" in design mode: When the state of an object changes, the object can notify other objects of the event.
To use the event model, you need to implement these three steps: 1, define events, 2, register event handles, 3, trigger events.
The subclass of Ccomponent defines an event by defining a member function that starts with on, such as the public Function OnClick () {...}, You then register the event handle by calling the Attacheventhandler member function (you can register multiple event handles), and finally trigger the event by calling RaiseEvent.
The Ccomponent class uses a private member variable to hold the event and handle all the handles of the event, which can be considered 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 handler list.
3. Behavior Class Binding
There are two ways to add attributes to a class: 1, directly modify the code for this class, add some member functions and member variables, 2, derive, extend through subclasses. It is obvious that the second method is more maintainable and easy to expand. If you need to add multiple attributes to a class (multiple people at different times), then you need to do a multilevel derivation, which obviously increases maintenance costs.
Ccomponent uses a special way to extend the class information-the behavior class binding. The behavior class is a Cbehavior subclass, Ccomponent can add member functions and member variables of one or more cbehavior classes to themselves, and unload some cbehavior classes when they are 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 ();
Add a calculator function to my class
$comp->attachbehavior (' Calculator ', new calculator ());
$comp->add (2, 5);
$comp->sub (2, 5);
Ccomponent through the 3 magic methods of __get, __set and __call (the use of these three magic methods, see: PHP Basics and Object 13--overloading) to implement the "Behavior class binding" feature, When calling a member variable and a member method that does not exist in the Ccomponent class, the Ccomponent class searches through the three magic methods on the dynamically bound behavior object. The non-existent member variables and member methods are routed to the dynamic bound object.
You can summarize the characteristics of the Ccomponent class in 3 words:
1, better configure an object, when set the object's member variable, actually is to run a piece of code;
2, better listen to an object, when the internal state of the object changes, other objects can be notified;
3, a better extension of an object, you can add member variables and member functions to an object, but also listen to the state of the object.
Yii Framework Analysis (v)--Talk about Ccomponent basic class