[nycphp-talk] mystery parameter for process method in PEAR, HTML_QuickForm
Dan Cech
dcech at phpwerx.net
Wed Jun 16 13:30:33 EDT 2004
Adam Maccabee Trachtenberg wrote:
> On Wed, 16 Jun 2004, Dan Cech wrote:
>>The idea of variable functions and using a variable as a function name
>>is kind of weird, but can also allow you to do some cool tricks.
>
> I actually find myself more frequently using call_user_func_array()
> than plain old call_user_func() because it allows you to more easily
> call functions with an arbitrary number of parameters.
>
> In particular, it's super useful for object aggregation in PHP 5 when
> you invoke it within __call().
If you like call_user_func_array then you will love safe_args, I've been
working on it with Dan Kuykendall (of phpGroupWare) as a different way
of handling function arguments.
For example, say you had a function like:
function myfunc ($a, $b, $c)
{
...
}
When defining the function it allows you to define which arguments are
mandatory and which are optional, default values for optional arguments
and acceptable data types.
The code using safe_args would look like:
function myfunc ()
{
$args = new safe_args();
$args->set('a',REQUIRED,'any');
$args->set('b',REQUIRED,'any');
$args->set('c',REQUIRED,'any');
$args = $args->get($func_get_args());
...
}
You then access each argument within the function as $args['a'],
$args['b'], etc.
As you can see above the definition for each argument includes the name,
the default value (or REQUIRED) and the type.
When calling the function above you could call it with:
myfunc($my_a,$my_b,$my_c);
or
myfunc(array($my_a,$my_b,$my_c));
or
myfunc(array('b'=>$my_b,'a'=>$my_a,'c'=>$my_c));
It took a little getting used to, but now I love the flexibility it
offers, as well as the ability to enforce input validation rules with
little effort.
If you are interested you can read more details at:
http://wiki.phpgroupware.org/index.php?page=nextgen
And view the source at:
http://savannah.gnu.org/cgi-bin/viewcvs/phpgroupware/api/core_functions.inc.php?rev=1.1.1.1.2.23&content-type=text/vnd.viewcvs-markup
Dan
More information about the talk
mailing list