title: Call anonymous function from db
body: <p>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,</p>
  
  <pre><code>**Event model in Yii**
  id - int
  name - varchar(255)
  beforeEvent - text
  </code></pre>
  
  <p>In "afterEvent" field stored function:</p>
  
  <pre><code> function(){echo 'afterEvent #1';}
  </code></pre>
  
  <p>Then i fetch all events and try call "afterEvent" function:</p>
  
  <pre><code>$events     = Event::model()-&gt;findAll();
  foreach ($events as $event)
  {
     $event-&gt;beforeEvent();                            
  }
  </code></pre>
  
  <p>As i haven't that method in model <strong>Event</strong>, i made proxy method</p>
  
  <pre><code>public function beforeEvent() { call_user_func($this-&gt;beforeEvent); }
  </code></pre>
  
  <p>Problem in error:</p>
  
  <blockquote>
    <p>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</p>
  </blockquote>
  
  <hr>
  
  <p>Found <strong>1 solution</strong>:
  in proxy-method beforeEvent() use <strong>create_function</strong> function like this:</p>
  
  <pre><code>public function beforeEvent(array $params){
  $callback = create_function('$params', $this-&gt;beforeEvent);
  $callback($params);
  }
  </code></pre>
  
  <p>but in manual said:</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>and we khow that eval is evil. How can i use anonymous function in this situation?</p>