Home  >  Article  >  Backend Development  >  PHP development framework Yii Framework tutorial (6) CComponent component

PHP development framework Yii Framework tutorial (6) CComponent component

黄舟
黄舟Original
2017-01-21 09:48:501175browse

The GameController defined in Hangman uses some attributes word. You can use the format of $this->word to read and write this attribute, but in fact the method corresponding to this attribute 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, define an attribute word that supports read and write operations by defining getWord and setWord (the name is case-insensitive). When the attribute is read, getWord() will be called, and its return value becomes the attribute value; Similarly, when writing properties, 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. An advantage of defining a property using getter and setter methods is that when the property is read or written, additional logic can be performed (e.g., performing validations, triggering events).
Base class CComponent In addition to defining properties, it also supports triggering events, which is very similar to UI components in ASP.Net.

Component events are special properties that use methods called event handlers as their values. Attaching (assigning) a method to an event will cause the method to be automatically called when the event is raised. Therefore, the behavior of a component may be modified in a way that was unforeseen during component development.

Component events are defined with a naming method starting with on. Like properties defined through getter/setter methods, event names are case-insensitive. The following code defines an onClicked event:

public function
onClicked($event){$this->raiseEvent('onClicked', $event);}

The $event used as the event parameter here is an instance of CEvent or its subclass.

We can attach a method to this event as follows:

$component->onClicked=$callback;

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

The structure of the event handler is as follows:

function methodName($event)
{
......
}

$event here That is, the parameters describing the event (which originate from the raiseEvent() call). The $event parameter is an instance of CEvent or one of its subclasses. At a minimum, it contains information about who triggered the event.

Starting from version 1.0.10, the event handler can also be an anonymous function supported by PHP 5.3 or later. For example,

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

If we call onClicked() now, the onClicked event will be fired (in onClicked()) and the attached event handler will be automatically called.

An event can be bound to multiple handles. When an event fires, these handlers will be executed in the order in which they were bound to the event. If the handler decides to prevent subsequent handlers from being executed, it can set $event->handled to true.

Starting from version 1.0.2, the component has added support for mixins and can bind one or more behaviors. A behavior is an object whose methods can be inherited by the component it is bound to by collecting functions, rather than specialized inheritance (i.e. ordinary class inheritance). A component can be inherited in a 'multiple inheritance' way Implement binding of multiple behaviors.

The behavior class must implement the IBehavior interface. Most behaviors can be inherited from CBeavior . If a behavior needs to be bound to a model, it can also inherit from CModelBehavior or CActiveRecordBehavior which implement binding features specifically for the model.

To use a behavior, it must first be bound to a component by calling this behavior's attach() method. Then we can call this behavior method through the component:

// $name 在组件中实现了对行为的唯一识别
$component->attachBehavior($name,$behavior);// test() 是行为中的方法。
$component->test();已绑定的行为可以像一个组件中的普通属性一样访问。 例如,如果一个名为 tree 的行为绑定到了一个组件,我们就可以通过如下代码获得指向此行为的引用。
$behavior=$component->tree;// 等于下行代码:
// $behavior=$component->asa('tree');行为是可以被临时禁止的,此时它的方法开就会在组件中失效.例如:
$component->disableBehavior($name);// 下面的代码将抛出一个异常
$component->test();
$component->enableBehavior($name);// 现在就可以使用了

$component->test(); It is possible for two behaviors with the same name to be bound to the same component. In this case, first Binding actions take precedence.

When used 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 events of the component. In this way, behavior can be observed organically or change the component's regular execution flow.

Since version 1.1.0, a behavior's properties can also be accessed through the component it is bound to. These properties include public member variables and properties set via getters and/or setters. For example, if a behavior has a property xyz and this behavior is bound to component $a, then we can access the property of this behavior using the expression $a->xyz.

The above is the content of the PHP development framework Yii Framework tutorial (6) CComponent component. 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