|
Posted by F Laupretre on 11/19/36 11:45
> 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/functions.arguments.php#functions.variable-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.
Navigation:
[Reply to this message]
|