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

Rodrigo Moraes rodrigo.moraes at gmail.com
Thu Aug 2 11:54:22 CDT 2007


On 8/2/07, Pierre Oztel wrote:
>
> 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.


Hi, Pierre.
I commented about this some time ago on IRC: that specific piece of code on
the Page_Controller is not used many times per request, so it's probably an
optimization with little gain. I think that it *could* make a difference
applied to validation callbacks, Solar_Form and View (to call helpers),
where call_user_func*() is used in a much bigger number.

Anyway, I personally dislike the workaround a bit because it is not elegant
imo, but extracting it from the Page_Controller to a place where it could be
used more times makes sense. :)

-- rodrigo
PS: I'm dropping below a refactored version (geez, I love callbacks)

public static function callback($exec) {
    if (! is_callable($exec)) {
        throw Solar::exception(
            'Solar',
            'ERR_INVALID_CALLBACK',
            'Invalid callback',
            array('callback' => $exec)
        );
    }

    // Get the additional arguments.
    $args = func_get_args();
    // Drop $exec.
    array_shift($args);

    if (is_array($exec)) {
        if (! is_object($exec[0])) {
            // Static method call.
            return call_user_func_array($exec, $args);
        } else {
            // Instance method call.
            $obj = $exec[0];
            $method = $exec[1];

            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 {
        // Simple function call.
        switch(count($args)) {
            case 0:
                return $exec();
                break;

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

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

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

            default:
                return call_user_func_array($exec, $args);
                break;
        }
    }
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman-mail3.webfaction.com/pipermail/solar-talk/attachments/20070802/ea101d64/attachment-0001.html


More information about the Solar-talk mailing list