PHP Development Framework YII Framework Tutorial (6) Ccomponent components

Source: Internet
Author: User
The gamecontroller that are defined in hangman are used to some properties of word, which can be read and written using the $this->word format, but in fact the method that corresponds to this property in Gamecontroller 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);}

That is, by defining Getword, Setword defines a property that supports read and write operations. Word (the name is case insensitive). When the property is read, Getword () is called and its return value becomes the property value; Similarly, when the property is written, the Setword () is called. If the setter method is not defined, the property will be read-only and an exception will be thrown if written to it. There is an advantage to defining a property using getter and Setter methods: When you read or write an attribute, you can perform additional logic (for example, perform validation, trigger an event).
The base class Ccomponent also supports triggering events in addition to defining attributes, and is very similar to the UI component in ASP.

Component events are special properties that use a number of methods, called event handlers, as their values. attaching (assigning) a method to an event will cause the method to be called automatically at the point where the event is aroused. As a result, the behavior of a component may be modified in a way that is not foreseen in the development process of the part.

Component events are defined as a naming method that starts with on. The name of the event is case insensitive, just as the attribute is defined by the Getter/setter method. The following code defines a onclicked event:

Public functiononclicked ($event) {$this->raiseevent (' onclicked ', $event);}

The $event here as an event argument is an instance of CEvent or its subclasses.

We can attach a method to this event, as follows:

$component->onclicked= $callback;

The $callback here points to a valid PHP callback. It can be a global function, or it can be a method in a class. If the latter, it must be provided as an array: Array ($object, ' methodName ').

The structure of the event handle is as follows:

function MethodName ($event) {...}

The $event here is the parameter that describes the event (it originates from the RaiseEvent () call). The $event parameter is an instance of CEvent or its subclasses. At a minimum, it contains information about who triggered this event.

Starting with version 1.0.10, the event handle can also be an anonymous function that is supported after PHP 5.3. For example

$component->onclicked=function ($event) {...}

If we call onclicked () now, the OnClicked event will be triggered (in onclicked ()), and the attached event handle will be automatically called.

An event can bind multiple handles. When an event is triggered, these handles are executed sequentially in the order in which they are bound to the event. If the handle determines that the subsequent handle of the organization is executed, it can be set $event->handled to True.

Starting with version 1.0.2, the component has added support for mixin and can bind one or more behaviors. A behavior is an object whose method can be inherited (inherited) by a collection of functions by its bound parts, rather than by proprietary inheritance (that is, ordinary class inheritance). A component can implement multiple behavior bindings in a ' multiple inheritance ' manner.

The behavior class must implement the Ibehavior interface. Most behaviors can inherit from Cbehavior. If a behavior needs to be bound to a model, it can also inherit from Cmodelbehavior or Cactiverecordbehavior, which implements the binding attribute specifically for the model.

To use a behavior, it must first be bound to a component by calling the Attach () method of this behavior. Then we can call this behavior method from the component:

$NAME implements the unique identification $component->attachbehavior ($name, $behavior) of the behavior in the component, and/or test () is the method in the behavior. $component->test (); The bound behavior can be accessed like a normal property in a component. For example, if a behavior named tree is bound to a component, we can get a reference to this behavior by using the following code. $behavior = $component->tree;//equals the following code://$behavior = $component->asa (' tree '), and the behavior can be temporarily banned, at which point its method is invalidated in the component. For example: $ Component->disablebehavior ($name);//The following code throws an exception $component->test (); $component->enablebehavior ($name) ;//You can use it now.

$component->test (); It is possible to bind two identically named behaviors to the same component. In this case, the first bound behavior has precedence.

When used in conjunction with events, the behavior is more powerful. When the behavior is bound to a component, some of the methods in the behavior can be bound to some of the components ' events. In this way, the behavior organically observes or alters the routine execution process of the component.

Starting with version 1.1.0, a property of a behavior can also be accessed by the component to which it is bound. These properties contain public member variables and properties that are set by getters and/or setters methods. For example, if a behavior has a property of XYZ, this behavior is bound to the component $a, and then we can use the expression $a->xyz to access the properties of this behavior.

The above is the PHP Development Framework Yii Framework Tutorial (6) Ccomponent component content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.