PHP development framework YiiFramework tutorial (6) CComponent component

Source: Internet
Author: User
PHP development framework YiiFramework tutorial (6) CComponent component

The GameController defined in Hangman uses some attribute word. you can use the $ this-> word format to read and write this attribute. However, in fact, the method that GameController corresponds to this attribute is

/**
* @ Return string the word to be guessed. This value is persistent
* During the whole game session.
*/
Public function getWord ()
{
Return $ this-> getPageState ('word ');
}

/**
* @ Param string the word to be guessed. This value is persistent
* During the whole game session.
*/
Public function setWord ($ value)
{
$ This-> setPageState ('word', $ value );
} Defines a word that supports read/write operations by defining getWord and setWord (the name is case insensitive ). when reading an attribute, getWord () is called, and its return value is a property value. Similarly, when writing an attribute, setWord () is called. If the setter method is not defined, the property will be read-only. if you write data to it, an exception will be thrown. Using the getter and setter methods to define an attribute has the benefit of executing additional logic (for example, performing verification and triggering an event) when reading or writing an attribute ).
In addition to defining attributes, the base class CComponent also supports trigger events, which are similar to the UI components in ASP. Net.

Component events are special attributes that use methods called event handlers as their values. Attaching (allocating) a method to an event will cause the method to be automatically called when the event is triggered. Therefore, the behavior of a component may be modified in an unpredictable way during part development.

Component events are defined in the naming method starting with on. The event name is case-insensitive, just like the naming method defined by properties using the getter/setter method. The following code defines an onClicked event:

Public function

OnClicked ($ event)
{
$ This-> raiseEvent ('onclicked', $ event );
} $ Event as the event parameter is an instance of CEvent or its subclass.

We can append a method to this event, as shown below:

$ Component-> onClicked = $ callback;
$ Callback points to a valid PHP callback. It can be a global function or a method in the class. If it is the latter, it must be provided as an array: array ($ object, 'methodname ').

The structure of the event statement handle is as follows:

Function methodName ($ event)
{
......
} $ Event here is the parameter describing the event (which is called by raiseEvent ). The $ event parameter is an instance of CEvent or its subclass. It contains information about who triggered the event.

From version 1.0.10, the event handle can also be an anonymous function supported by PHP 5.3 and later versions. For example,

$ Component-> onClicked = function ($ event ){
......
} If we call onClicked () now, the onClicked event will be triggered (in onClicked (), and the affiliated event handle will be automatically called.

You can bind multiple handles to an event. When an event is triggered, these handles are executed sequentially according to the order in which they are bound to the event. If the handle determines that the subsequent handle is organized, it can set $ event-> handled to true.

Starting from version 1.0.2, the component has added support for mixin and can be bound to one or more actions. Behavior is an object. its method can be inherited by the components it binds by using the collection function, rather than proprietary inheritance (that is, normal class inheritance ). one part can bind multiple behaviors in the 'multi-inheritance 'mode.

The behavior class must implement the IBehavior interface. Most actions can be inherited from CBehavior. If an action needs to be bound to a model, it can also inherit from CModelBehavior or CActiveRecordBehavior, which is designed for model binding.

To use an action, it must first bind it to a component by calling the attach () method of this behavior. Then we can call this behavior method through the component:

// $ Name uniquely identifies behavior in components
$ Component-> attachBehavior ($ name, $ behavior );
// Test () is the method in the action.
$ Component-> test (); bound behavior can be accessed like common attributes in a component. For example, if a behavior named tree is bound to a component, we can use the following code to obtain a reference pointing to this behavior.

$ Behavior = $ component-> tree;
// Equals the downstream code:
// $ Behavior = $ component-> asa ('tree ');
The behavior can be temporarily disabled. in this case, its method will become invalid in the component. for example:

$ Component-> disableBehavior ($ name );
// The following code will throw an exception
$ Component-> test ();
$ Component-> enableBehavior ($ name );
// You can use it now
$ Component-> test (); it is possible to bind two behaviors with the same name to the same component. in this case, the first binding action has priority.

When used together with events, the behavior will be more powerful. when a behavior is bound to a component, some methods in the behavior can be bound to some components of the component. in this way, the behavior has the opportunity to observe or change the general execution process of the component.

Starting from version 1.1.0, a behavior attribute can also be accessed by the bound component. These attributes include public member variables and attributes set through getters and/or setters. For example, if an action has an xyz attribute, this behavior is bound to component $ a, and then we can use the expression $ a-> xyz to access the attribute of this behavior.


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.