I’m actively learning Yii, now into Components. I read the Components Chapter twice, still I couldn’t understand at situation Events/Behavior. With browsers, users generate events by clicking on elements, we intercept such events and run our code and optionally let the event to continue with its default event. How come a server side component can generate event?
Let’s say I have created Facebook Component and want to raise an event when user likes my page? Users like at browser side so does this event captures through Ajax at Yii’s component side to handle the event?
It would be great, if someone can explain Events/Behavior with some real world example and sample code.
Try not to think of it having anything to do with user interaction because they aren’t really user events. It’s a programming paradigm.
Sometimes you don’t want to have to redesign an inheritance tree to add a new behaviour (method) to a class, so you use behaviours to implement mixins. Mixins are like multiple inheritance so for example you have Fruit and you want an Apple but you also want Apple to have all the methods that Snack has. You first make Apple extend Fruit but then also attach Snack as a behaviour. PHP wont actually see Apple as a Snack but it will have all of Snack’s methods. It’s an interesting alternative (in some cases) to composition.
Events are more akin to hooks in some other frameworks/languages where you can have events be raised programmatically and then execute some code you have attached to that hook. You can implement these yourself or attach code to existing ones.
Check this out for more:
Wiki Behaviours and Events
Wouldn’t hurt to take a look at the theory behind Mixins and Hooks.