Passing arguments to callbacks

This is a discussion on Passing arguments to callbacks within the PHP Language forums, part of the PHP Programming Forums category; No point re-inventing the wheel, so thought I'd see if anybody's got some good example code kicking ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-21-2006
Mary Pegg
 
Posts: n/a
Default 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($callback) {
if is_array($callback_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?

Reply With Quote
  #2 (permalink)  
Old 04-21-2006
F Laupretre
 
Posts: n/a
Default Re: Passing arguments to callbacks

> Q1. Is testing callback against NULL a sensible way of implementing an
> optional callback?


Yes, null is fine as default value in this case, but I would use isnull()
instead of a plain compare.

> Q2. How does that look, PHP people?


You don't have to pass an array. PHP implements variable length argument
lists. Have a look at
http://www.php.net/manual/en/functio...iable-arg-list
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,$callback=NULL)
{
if (isnull($callback)) return $a*2; // default
if (!is_callable($callback)) throw new Exception('Invalid callback');
$args=get_func_args();
$args=(count($args)>2) ? array_slice($args,2) : array();
return call_user_func_array($callback,$args);
}

> 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?


No. Definitely not.
Reply With Quote
  #3 (permalink)  
Old 04-21-2006
Mary Pegg
 
Posts: n/a
Default Re: Passing arguments to callbacks

F Laupretre wrote:

<snip>

Thanks for the feedback.

> and use func_get_args() to retrieve a variable number of arguments. Your


Yes, I thought about this but felt that if you are going to do this:

> $args=get_func_args();
> $args=(count($args)>2) ? array_slice($args,2) : array();
> return call_user_func_array($callback,$args);


you might as well just pass an array in anyway. Not sure now.
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 12:23 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0