views:

30

answers:

1

We are working on a Builder type interface that basically constructs a list for doing CRUD management of individual objects (since we're using ActiveRecord an object == a database record).

In order to make specifying the column values and parameters for list options flexible we originally implemented the callback arguments as an array that looked like this:

'params' => array(
              'static' => $this->institution->id, 
              array(
                'method1' => array(
                                'method_name', 
                                array('arg1', 'arg2')
                             ), 
                             'prop1' => 'id')

This solution presents some obvious drawbacks just in terms of awkwardness, etc. but it worked.

At the suggestion of a coworker, we have tried implementing closures but due to another requirement with the class, we have to serialize all of the data in the class which we only just discovered won't work with closures. The syntax with the closures was obviously much simpler:

'params' => array(
               $this->institution->id,
               function($obj) { return $obj->method_name($arg1, $arg2)->id; }
             )

So my first question is whether there is a way to store the closure in a serializable format (like a string) and then convert it back to a closure once the class is unserialized ?

Or can anyone provide a suggestion on a better interface for solving this sort of callback problem?

A: 

There is an excellent and detailed post on how to use Reflection to extend Closures to do what you want: Extending PHP 5.3 Closures with Serialization and Reflection. That said, I wonder if this is any less awkward than your original solution, since you'll have to pass objects instead - it effectively just codifies it slightly differently and you might just end up with a performance hit.

Hamish