When reading YIi guide at this link: http://www.yiiframework.com/wiki/327/events-explained/, I have see this line:
…So earlier before raising the event we should have called, maybe inside the initialization method of the component, something like this:
$myComponent->onForestRan = array(new SomeOtherClass, 'eventHandler1');
I understand that this code will attach a handler to event, and the array on the left-hand side is a PHP callback function. However, what I really don’t understand is its syntax, does it call a onforestRan() function (which is previously defined on $component–> see the Yii link above), if so it will not valid as it lack of $event argument. Or, if it is a callback, then I have never seen a way of using callback like this (if it is a callback where is call_user_func() or usort()..). Its syntax is really odd to me.
Could some one help me with this?
Thank so much!
It’s not a callback perse, but you’re telling Yii what action (i.e. what function) to perform on the event.
In the example given, when
onForestRanhappens, theeventHandler1function fromSomeOtherClasswill be triggered.When that page describes a callback, it’s saying that whatever you assign to
$myComponent->onForestRanneeds to be a callback function. The callback isn’t executed at that point, you’re just letting Yii know which callback(s) to use when the event occurs.More details:
onForestRan is a special property of $myComponent, as documented here. More details can be seen in CComponent. You can search the CComponent source for lines similar to line 113, where you can see the beginning of the logic for the special property. This is very similar to
action*methods in the controller.