[Solar-talk] Making a Solar::callback() method

Pierre Oztel pierre.oztel at gmail.com
Thu Aug 2 11:03:33 CDT 2007


It is too bad i cannot post my suggestion to the TRAC, Akismet just don't
let me in ...

Since call_user_func functions are so slow, and since we have many callback
in Solar, maybe it is a good idea to function refactor what you did in the
Controller of 0.28 version.


A quick example can be :

public static function callback($exec, $args = array()) {

    if(is_array($exec)) {
        if(count($exec) < 2) {
            throw new Exception('Invalid object callback');
        }
        $obj = array_shift($exec);
        $method = array_shift($exec);

        if(!method_exists($obj, $method)) {
            throw new Exception('not callable');
        }

        $args = (array) array_shift($info['default']);
        switch(count($args)) {
            case 0:

                return $obj->$method();
                break;
            case 1:

                return $obj->$method($args[0]);
                break;
            case 2:

                return $obj->$method($args[0], $args[1]);
                break;
            case 3:

                return $obj->$method($args[0], $args[1], $args[2]);
                break;
            default:

                return call_user_func_array(array($obj, $method), $args);
                break;
        }
    } else {
        //function callback
        $args = (array) array_shift($info['default']);
        if(!function_exists($exec)) {
            throw new Exception('no function');
        }
        switch(count($args)) {

            case 0:
                return $exec();
                break;
            case 1:
                return $exec($args[0]);
                break;
            case 2:

                return $exec($args[0], $args[1]);
                break;
            default:

                return call_user_func_array($exec, $args);
                break;
        }
    }
}

And wait for call_user_func_array to be optimized.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman-mail3.webfaction.com/pipermail/solar-talk/attachments/20070802/56ff863c/attachment.html


More information about the Solar-talk mailing list