Behaviors & events
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:
Yii::app()->onbeginRequest = create_function('$event', 'return ob_start("ob_gzhandler");'), 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:
$test_comp->onSomethingGoesOn = array(new SomeClass, 'eventHandler1'); $test_comp->onSomethingGoesOn = array(new SomeOtherClass, 'eventHandler2'); $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:
$test_comp->onSomethingGoesOn(new CEvent($this)); $test_comp->onSomethingGoesOn(new CEvent());
So, basically, it allows you 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:
class MyBoringClass extends MySuperClass1, MySuperClass2 { }
This is where behaviors come in. Instead, you can go:
class MyBoringClass extends MySuperClass1 { } $classInstance = new MyBoringClass(); $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 an 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.
class SomeClass extends CBehavior { public function add($x, $y) { return $x + $y; } }
Then use with:
$test_comp = new TestComponent(); $test_comp->attachbehavior('blah', new SomeClass); $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 it is 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).
Total 6 comments:
Gents, one question: how it is possible to add event handler from different modules. example: photo sharing site with modules register, login, core.
onRegister - a photo is created (by a method in core) onLogin - user photo is checked, if yes->something, if no->something else
still need real world examples to understand the purpose of behaviors and event better. This article doesn't make me understand better after reading the doc from the definitive guide.
as "still" said, missing a real world use case and samples for an Event that is not beforeSave, afterFind, etc...
I concur with yiimann and scoob. I read this cookbook, and I reread the entire component page, but the part with events is still unclear. I'm not certain where the event hook is placed or where/how the event attachment is processed from.
If I have a component, and it has a number of methods, if I want to place event hooks in them, all those methods need to begin with "onXXX"?
For example, when building an application, if there are some modules that are going to modify some event, when/where do they attach to the event?
Also, once the event is called, what data does it have access to? Are all the variables that existed in the method it was raised in available? Do they need to be returned? What if I want to event hooks at the beginning and end of a method?
Hello, I see that a lot of people, me included, need examples to better understand some new (for us anyway) concepts, and this is one of them, and cant find out why is so uncommon for the people who write this docs to write them. Don't get me wrong please! I'm very very greatfull, in fact, thanks for this, it helps a lot, it's only for you to realize how important, or at least helpful is to see some real life examples as companions to this docs. Please, consider this and other requests as they are very important for (I think) a lot of users like me that are trying, with some effort, to use this framework as it best! Thanks and sorry for my poor English
with respect. Juan

Most of this article has been written by auxbuss in this topic.
Feel free to correct or extend this article to better suit newbie's experiences.