Passing arguments to callbacks

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mary Pegg

    Passing arguments to callbacks

    No point re-inventing the wheel, so thought I'd see if
    anybody's got some good example code kicking around.

    (Please excuse trivial syntactical errors, it's all coming
    off the top of my head).

    Say I have a function thus:

    function munge($a) {
    return ($a * 2);
    }

    Now I extend it with a callback:

    function munge($a, $callback = NULL) {
    if ($callback != NULL) {
    $callback($a);
    }
    return ($a * 2);
    }

    function add_two ($in) {
    return ($in + 2);
    }

    then I would call munge(3, 'add_two'); to get 10.

    Q1. Is testing callback against NULL a sensible way of implementing an
    optional callback?
    Q1a. Should I replace it with an is_callable() or function_exists (), or
    augment it with one, and which of these two makes more sense here?
    (I'm guessing replace with is_callable()).

    I now want to introduce another callback:

    function add_var($in, $var) {
    return ($in + $var);
    }
    so that I can call
    munge(3);
    munge(3, 'add_two');
    munge(3, 'add_var', 1);
    to get results of 6, 10, and 8 respectively.

    And, of course, I want to have the number of parameters sent to the
    callback to be arbitrary.

    So, this line of thought leads me to call_user_func_ array();

    function munge($a, $callback = NULL, $callback_args = NULL) {
    if is_callable($ca llback) {
    if is_array($callb ack_args)
    call_user_func_ array($callback , $callback_args) ;
    else
    $callback($a);
    }
    return ($a * 2);
    }

    and I call
    munge(3, 'add_var', array(1));
    and
    munge(3, 'complicated', array(1,2,3));
    where complicated() is a function taking 4 parameters.

    Q2. How does that look, PHP people?
    Q2A. Would you add another couple of lines with "if isset" to allow the
    use of call_user_func( ) for callbacks that only take two parameters?

  • F Laupretre

    #2
    Re: Passing arguments to callbacks

    > Q1. Is testing callback against NULL a sensible way of implementing an[color=blue]
    > optional callback?[/color]

    Yes, null is fine as default value in this case, but I would use isnull()
    instead of a plain compare.
    [color=blue]
    > Q2. How does that look, PHP people?[/color]

    You don't have to pass an array. PHP implements variable length argument
    lists. Have a look at

    and use func_get_args() to retrieve a variable number of arguments. Your
    function could look like this (exceptions need PHP 5):

    function munge($a,$callb ack=NULL)
    {
    if (isnull($callba ck)) return $a*2; // default
    if (!is_callable($ callback)) throw new Exception('Inva lid callback');
    $args=get_func_ args();
    $args=(count($a rgs)>2) ? array_slice($ar gs,2) : array();
    return call_user_func_ array($callback ,$args);
    }
    [color=blue]
    > Q2A. Would you add another couple of lines with "if isset" to allow the
    > use of call_user_func( ) for callbacks that only take two parameters?[/color]

    No. Definitely not.

    Comment

    • Mary Pegg

      #3
      Re: Passing arguments to callbacks

      F Laupretre wrote:

      <snip>

      Thanks for the feedback.
      [color=blue]
      > and use func_get_args() to retrieve a variable number of arguments. Your[/color]

      Yes, I thought about this but felt that if you are going to do this:
      [color=blue]
      > $args=get_func_ args();
      > $args=(count($a rgs)>2) ? array_slice($ar gs,2) : array();
      > return call_user_func_ array($callback ,$args);[/color]

      you might as well just pass an array in anyway. Not sure now.

      Comment

      Working...