Tuesday, July 3, 2012

ways to define a callback in yii framework

Use a global function and just pass its name as a string, such as 'my_function'.
Use a static class method. You should pass an array: array('ClassName',
'staticMethodName') .
Use an object method: array($object, 'objectMethod').
Create and pass anonymous function using create_function as follows:
$component->attachEventHandler('onClick',
create_function('$event', 'echo "Click!";'));
Since PHP 5.3, you can use anonymous functions without create_function:
$component->attachEventHandler('onClick', function($event){
echo "Click!";
});To keep your code shorter, you can use component properties to manage event
handlers as follows:
$component->onClick=$handler;
// or:
$component->onClick->add($handler);
To manage event handlers more precisely, you can get handlers list (CList)
using CComponent::getEventHandlers and work with it. For example, you can
attach an event handler the same way as with attachEventHandler using the
following code:
$component->getEventHandlers('onClick')->add($handler);
To add an event handler to the beginning of handlers list, use:
$component->getEventHandlers('onClick')->insertAt(0, $handler);
To delete a particular handler you can use CComponent::detachEventHandler
as follows:
$component->detachEventHandler('onClick', $handler);
Alternatively, get a list of handlers as shown earlier and delete handlers from it.

No comments:

Post a Comment