You are here: Re: Passing arguments to callbacks « PHP Programming Language « IT news, forums, messages
Re: Passing arguments to callbacks

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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация