Re: function -> action

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2007/8/3, Ken Tozier <kentozier@xxxxxxxxxxx>:
>
>
> On Aug 3, 2007, at 9:39 AM, Ken Tozier wrote:
>
> >
> > On Aug 3, 2007, at 2:38 AM, Ralph Kutschera wrote:
> >
> >> Hallo!
> >>
> >>   I'm working on a project, where we distinguish between
> >> "functions" and
> >> "actions" in design, although in PHP both are implemented as
> >> functions.
> >> Is there a chance that PHP can use the word "action" as "function"?
> >>
> >> E.g.:
> >> public function doSomething() { .... }
> >> public action doSomethingElse() { ... }
> >>
> >> ... is actually the same but would help to see the design also in
> >> the code.
> >
> > You could define a generic action function
> >
> > function action($funcName, $funcArgs)
> > {
> >       return $funcName($funcArgs);
> > }
>
> Actually, this might be better as it makes no assumptions about the
> function's argument list.
>
> function action()
> {
>         $func           = func_get_arg(0);
>         $expr           = func_get_args();
>         $expr           = array_slice($expr, 1);
>         $expr           = implode(', ', $expr);
>         $expr           = $func.'('.$expr.');';
>
>         return eval($expr);
> }


Actually this function won't work as expected. The arguments are improperly
passed, they are not escaped and would only work with variables that hold
numeric values, one word strings that do no match to existing constants
(which will still trigger a notice in the error log). Also return values
will not be passed through, because a return statement was not included in
the eval'ed code.

eval is not the best solution for this:

function action() {
    $func = func_get_arg(0);
    $args = func_get_args();
    $args = array_slice($args, 1);
    return call_user_func_array($func, $args);
}


You lose the ability to pass by reference but that might not be
> important for your script.


That's not entirely true, objects are always references. You lose the
ability to pass any other type of value by reference.

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux