473,322 Members | 1,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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?

Apr 20 '06 #1
2 4593
> 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.
Apr 21 '06 #2
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.
Apr 21 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Raphael Bauduin | last post by:
Hi, I'm working with mozilla, building a little XUL app. I have a datasource, and want to display a message once it is loaded (datasource.loaded is true). So, I thought to use timouts, but as...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
3
by: johny smith | last post by:
I am having trouble with a basic concept and need some help. What I want to do is upon construction of an object I want to pass in a callback function name with an argument. The class then...
8
by: dakman | last post by:
Recently, I have been needing to do this alot and I can never find a way around it, the main reason I want to do this is because for example in the application I am making right now, it creates a...
39
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
9
by: zholthran | last post by:
Hi folks, after reading several threads on this issue (-> subject) I fear that I got a problem that cannot easily be solved by the offered workarounds in an acceptable way, at least not with my...
2
by: william.w.oneill | last post by:
I have an application that takes a few command line parameters. As recommended by others in this group, I'm using a named mutex to ensure that only one instance of the application is running. My...
40
by: Angus | last post by:
Hello I am writing a library which will write data to a user defined callback function. The function the user of my library will supply is: int (*callbackfunction)(const char*); In my...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.