I use Yii in my project and want realize event model with callbacks. I made "Event" model in MySQL and want store anonymous functions in many fields, for example,
**Event model in Yii**
id - int
name - varchar(255)
beforeEvent - text
In "afterEvent" field stored function:
function(){echo 'afterEvent #1';}
Then i fetch all events and try call "afterEvent" function:
$events = Event::model()->findAll();
foreach ($events as $event)
{
$event->beforeEvent();
}
As i haven't that method in model Event, i made proxy method
public function beforeEvent() { call_user_func($this->beforeEvent); }
Problem in error:
call_user_func() expects parameter 1 to be a valid callback, function 'function(){ echo 'afterEvent of event #1 done'; }' not found or invalid function name
Found 1 solution: in proxy-method beforeEvent() use create_function function like this:
public function beforeEvent(array $params){
$callback = create_function('$params', $this->beforeEvent);
$callback($params);
}
but in manual said:
This function internally performs an eval() and as such has the same security issues as eval(). Additionally it has bad performance and memory usage characteristics. If you are using PHP 5.3.0 or newer a native anonymous function should be used instead.
and we khow that eval is evil. How can i use anonymous function in this situation?