Introduction: This is a detailed page for yii framework cComponent source code analysis. It introduces PHP, related knowledge, skills, experience, and some PHP source code.
Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 339159 'rolling = 'no'>
CComponent source code analysis
// Base class of all parts
Class cComponent
{
Private $ _ E;
Private $ _ m;
// Magic Method for retrieving part attributes, events, and actions
Public Function _ Get ($ name)
{
$ Getter = 'get'. $ name;
// Whether the get method of the attribute exists
If (method_exists ($ this, $ getter ))
Return $ this-> $ getter ();
// Obtain the event processing handle starting with on
Else if (strncasecmp ($ name, 'on', 2) ===0 & method_exists ($ this, $ name ))
{
// Lowercase event name
$ Name = strtolower ($ name );
// If _ e [$ name] does not exist, an empty clist event handle queue object is returned.
If (! Isset ($ this-> _ e [$ name])
$ This-> _ e [$ name] = new clist;
// Return the handle queue object stored in _ e [$ name]
Return $ this-> _ e [$ name];
}
// _ M [$ name] is returned if the behavior object is stored in it
Else if (isset ($ this-> _ m [$ name])
Return $ this-> _ m [$ name];
Else
Throw new cexception (yii: T ('yii', 'Property "{class}. {property}" is not defined .',
Array ('{class}' => get_class ($ this), '{property}' => $ name )));
}
/**
* PHP magic Method
* Set attributes and events of a component
*/
Public Function _ set ($ name, $ value)
{
$ Setter = 'set'. $ name;
// Whether the set method of the attribute exists
If (method_exists ($ this, $ setter ))
$ This-> $ setter ($ value );
// Name starts with on. This is the event processing handle.
Else if (strncasecmp ($ name, 'on', 2) ===0 & method_exists ($ this, $ name ))
{
// Lowercase event name
$ Name = strtolower ($ name );
// _ E [$ name] creates a clist object if it does not exist.
If (! Isset ($ this-> _ e [$ name])
$ This-> _ e [$ name] = new clist;
// Add event handling handle
$ This-> _ e [$ name]-> Add ($ value );
}
// The property does not have the set method. Only the get method is used. It is a read-only property and an exception is thrown.
Else if (method_exists ($ this, 'get'. $ name ))
Throw new cexception (yii: T ('yii', 'Property "{class}. {property}" is read only .',
Array ('{class}' => get_class ($ this), '{property}' => $ name )));
Else
Throw new cexception (yii: T ('yii', 'Property "{class}. {property}" is not defined .',
Array ('{class}' => get_class ($ this), '{property}' => $ name )));
}
/**
* PHP magic Method
* Provides the isset () function with an attribute and event processing handle.
*/
Public Function _ isset ($ name)
{
$ Getter = 'get'. $ name;
If (method_exists ($ this, $ getter ))
Return $ this-> $ getter ()! = NULL;
Else if (strncasecmp ($ name, 'on', 2) ===0 & method_exists ($ this, $ name ))
{
$ Name = strtolower ($ name );
Return isset ($ this-> _ e [$ name]) & $ this-> _ e [$ name]-> getcount ();
}
Else
Return false;
}
/**
* PHP magic Method
* Set the property value to null or delete the processing handle corresponding to the event name
*/
Public Function _ unset ($ name)
{
$ Setter = 'set'. $ name;
If (method_exists ($ this, $ setter ))
$ This-> $ setter (null );
Else if (strncasecmp ($ name, 'on', 2) ===0 & method_exists ($ this, $ name ))
Unset ($ this-> _ e [strtolower ($ name)]);
Else if (method_exists ($ this, 'get'. $ name ))
Throw new cexception (yii: T ('yii', 'Property "{class}. {property}" is read only .',
Array ('{class}' => get_class ($ this), '{property}' => $ name )));
}
/**
* PHP magic Method
* CComponent undefined class Methods: find methods with the same name in the behavior class to call behavior methods.
*/
Public Function _ call ($ name, $ parameters)
{
// The $ _ m array of the behavior class is not empty.
If ($ this-> _ m! = NULL)
{
// Cyclically retrieve the behavior classes stored in the $ _ m Array
Foreach ($ this-> _ m as $ object)
{
// The action class object is valid and the method exists.
If ($ object-> enabled & method_exists ($ object, $ name ))
Return call_user_func_array (Array ($ object, $ name), $ parameters );
}
}
Throw new cexception (yii: T ('yii', '{class} does not have a method named "{name }".',
Array ('{class}' => get_class ($ this), '{name}' => $ name )));
}
/**
* Return behavior Objects Based on the behavior name
*/
Public Function ASA ($ behavior)
{
Return isset ($ this-> _ m [$ Behavior])? $ This-> _ m [$ Behavior]: NULL;
}
/**
* Attaches a list of behaviors to the component.
* Each behavior is indexed by its name and shocould be an instance
* {@ Link ibehavior}, a string specifying the behavior class, or
* Array of the following structure:
* <PRE>
* Array (
* 'Class' => 'path. to. behaviorclass ',
* 'Property1' => 'value1 ',
* 'Property2' => 'value2 ',
*)
* </PRE>
* @ Param array list of behaviors to be attached to the component
* @ Since 1.0.2
*/
Public Function attachbehaviors ($ behaviors)
{
// $ Behaviors is an array $ name => $ Behavior
Foreach ($ behaviors as $ name => $ behavior)
$ This-> attachbehavior ($ name, $ behavior );
}
/**
* Add a behavior to the component.
*/
Public Function attachbehavior ($ name, $ behavior)
{
/* $ Behavior is not an instance of the ibehavior interface, it is
* Array (
* 'Class' => 'path. to. behaviorclass ',
* 'Property1' => 'value1 ',
* 'Property2' => 'value2 ',
*)
* Passed to yii: createcomponent. The object property is initialized.
*/
If (! ($ Behavior instanceof ibehavior ))
$ Behavior = yii: createcomponent ($ behavior );
$ Behavior-> setenabled (true );
$ Behavior-> attach ($ this );
Return $ this-> _ m [$ name] = $ behavior;
}
/**
* Raises an event.
* This method represents the happening of an event. It invokes
* All attached handlers for the event.
* @ Param string the event name
* @ Param cevent the event Parameter
* @ Throws cexception if the event is undefined or an event handler is invalid.
*/
Public Function raiseevent ($ name, $ event)
{
$ Name = strtolower ($ name );
// _ E [$ name] The event processing handle queue exists.
If (isset ($ this-> _ e [$ name])
{
// Cyclically retrieve the event processing handle
Foreach ($ this-> _ e [$ name] as $ handler)
{
// The event processing handle is a global function.
If (is_string ($ handler ))
Call_user_func ($ handler, $ event );
Else if (is_callable ($ handler, true ))
{
// An array: 0-object, 1-method name
List ($ object, $ method) = $ handler;
If (is_string ($ object) // static Class Method
Call_user_func ($ handler, $ event );
Else if (method_exists ($ object, $ method ))
$ Object-> $ method ($ event );
Else
Throw new cexception (yii: T ('yii', 'event "{class}. {event}" is attached with an invalid handler "{handler }".',
Array ('{class}' => get_class ($ this), '{event}' => $ name, '{handler}' => $ handler [1]);
}
Else
Throw new cexception (yii: T ('yii', 'event "{class}. {event}" is attached with an invalid handler "{handler }".',
Array ('{class}' => get_class ($ this), '{event}' => $ name, '{handler}' => GetType ($ handler ))));
// $ Event handled is set to true to stop the call of the remaining handle in the queue
If ($ event instanceof cevent) & $ event-> handled)
Return;
}
}
Else if (yii_debug &&! $ This-> hasevent ($ name ))
Throw new cexception (yii: T ('yii', 'event "{class}. {event}" is not defined .',
Array ('{class}' => get_class ($ this), '{event}' => $ name )));
}
} Transferred from: http://blog.163.com/zyf_win/blog/static/1220628922010544612617/
Love J2EE follow Java Michael Jackson video station JSON online tools
Http://biancheng.dnbcw.info/php/339159.html pageno: 8.