手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆

Yii 官方关于event和behaviors的文章

首页 > PHP Framework >

本文内容来自官方的wiki,一来是做备份,二来自己查询起来也会快一点,毕竟yiiframework官网经常抽风,当然它的抽风不是因为自己,而是因为GFW,说来也可怜,好象很多技术网站都被墙了。
不过想想也正常,如果一个搞技术的,连翻墙也不会,还搞个毛技术啊。
OK,上正菜:http://www.yiiframework.com/wiki/44/behaviors-events/#hh1

These features provide endless possibilities and unbelievable flexibility, but as current documentation does not give more than a few examples, it might be difficult to fully understand their internals and requirements.

It should be noted that they do mostly the same thing. You can attach behaviors and event handlers to components to modify the components' behavior.

Events

It is useful when you want to interrupt the normal application flow without extending base classes.

For example, enabling gzip compression on the output could be done via extending CWebApplication. But because there are entry points for event handlers, one can do this:

PHP代码
  1. Yii::app()->onbeginRequest = create_function('$event''return ob_start("ob_gzhandler");'),  
  2. Yii::app()->onendRequest = create_function('$event''return ob_end_flush();'),  

You can create an event handler -- which is simply a method in some class with a specific signature -- and attach it to the event of an object. You can add as many event handlers as you wish, from as many objects as you wish. If the event handler is, effectively static, then you can create the object as you assign it:

PHP代码
  1. $test_comp->onSomethingGoesOn = array(new SomeClass, 'eventHandler1');  
  2. $test_comp->onSomethingGoesOn = array(new SomeOtherClass, 'eventHandler2');  
  3. $test_comp->onSomethingGoesOn = array(new YetAnotherClass, 'eventHandler3');  

As long as you have a handle on the object, then you can add an event handler to it.

At some point, you can then raise the event with something like one of these:

PHP代码
  1. $test_comp->onSomethingGoesOn(new CEvent($this));  
  2. $test_comp->onSomethingGoesOn(new CEvent());  

So, basically, it allows you to build a list of function calls that can later be executed, in the order they were added. It can save you passing around a lot of object refs and building conditional code, since you can still raise the event, even if it doesn't do anything.

Behaviors

Behaviors are simply a way of adding methods to an object.

Take this scenario: You have 2 classes: MySuperClass1, MySuperClass2. There might be lots of methods from MySuperClass1 & 2 that you want in some new class, say MyBoringClass. Unfortunately, php does not allow for this:

PHP代码
  1. class MyBoringClass extends MySuperClass1, MySuperClass2 {  
  2. }  

This is where behaviors come in. Instead, you can go:

PHP代码
  1. class MyBoringClass extends MySuperClass1 {  
  2. }  
  3.    
  4. $classInstance = new MyBoringClass();  
  5. $classInstance->attachbehavior('uniqueName'new MySuperClass2);  

Now $classInstance has all the methods from MySuperClass1 and MySuperClass2. Since MySuperClass2 is being used as a behavior, it has to extend CBehavior. The only caveat to this is an attached behavior cannot override any class methods of the component it is being attached to. If a method already exists, if it be from the original class or already added by a previously attached behavior, it will not be overwritten.

In an OO language like Ruby, it's quite possible to start with a completely empty object and simply build its behavior as you go along. Yii provides this behavior with a little magic. The key is that the class you wish to add the behavior from must extend Cbehavior.

PHP代码
  1. class SomeClass extends CBehavior  
  2. {  
  3.     public function add($x$y) { return $x + $y; }  
  4. }  

Then use with:

PHP代码
  1. $test_comp = new TestComponent();   
  2. $test_comp->attachbehavior('blah'new SomeClass);  
  3. $test_comp->add(2, 5);  

So, in this case, you are extending the functionality of an object with functionality of another object.

After studying this cookbook page you are encouraged to reread the corresponding guide page as it contains advanced information (for example, if you are familiar with interfaces, you might find it enough to implement IBehavior before extending CBehavior).

-------------

behavior和Event在model里用的会相对较多一点,不过,其他地方也可以一用。如果你真的不明白这一块的逻辑,那还是用其他方式替代吧。

 

 

 

 

 

 

 

 




本站采用创作共享版权协议, 要求署名、非商业和保持一致. 本站欢迎任何非商业应用的转载, 但须注明出自"易栈网-膘叔", 保留原始链接, 此外还必须标注原文标题和链接.

Tags: yii, behavior, event

« 上一篇 | 下一篇 »

只显示10条记录相关文章

常用网站的反向代理页[2013-09-28] (浏览: 68785, 评论: 10)
Yii CDbCriteria的常用方法 (浏览: 57293, 评论: 5)
将Yiiframework与JQuery easyUI整合使用 (浏览: 39038, 评论: 2)
Yii:relations update(self::STAT) (浏览: 34872, 评论: 0)
值得收藏的yii2的doc中关于db Query的说明 (浏览: 30659, 评论: 0)
Yii Demos 随想 (浏览: 29638, 评论: 3)
Yii ClinkPager 郁闷 (浏览: 28496, 评论: 2)
在Yii框架中使用Hprose或PHPRPC (浏览: 28419, 评论: 0)
Yiiframework(Yii框架)开发笔记:续四 (浏览: 27642, 评论: 3)
Yii 一行代码,为模块绑定子域名 (浏览: 26842, 评论: 0)

发表评论

评论内容 (必填):