Page 1 of 1

Possible to send multiple params to callback?

Posted: Wed Jan 10, 2007 3:46 pm
by Luke
I can't seem to find this in the manual. I've never had to send multiple parameters to a callback before, but I'm trying to do essentially this:

Code: Select all

$form->addRule(
    'account_username',
    'Username may only contain alpha-numeric characters',
    'callback',
    array('Zend_Filter', 'isRegex'), array($value, $pattern)
);
so array('Zend_Filter', 'isRegex') would be the callback, and array($value, $pattern) would be the callback params... how would I do this?

Posted: Wed Jan 10, 2007 5:45 pm
by volka
That really depends on the code using the callback (and the parameters). addRule is a function of what class?

Posted: Wed Jan 10, 2007 5:47 pm
by Luke
umm... HTML_QuickForm::addRule()

It doesn't look like it's possible. I don't know what I was thinking, it entirely depends on that library, thanks though. :)

Posted: Wed Jan 10, 2007 5:57 pm
by volka
But you can pass the array as one parameter to a function that calls another function with the array elements as single parameters

Code: Select all

<?php
function twoParameters($a, $b) {
	echo 'a:', $a, "<br />\nb:", $b;
}

function oneParameter($a) {
	twoParameters($a[0], $a[1]);
}

$value='v'; $pattern='p';
call_user_func('oneParameter', array($value, $pattern));
or with a anonymous trampoline function

Code: Select all

<?php
function twoParameters($a, $b) {
	echo 'a:', $a, "<br />\nb:", $b;
}
$value='v'; $pattern='p';
call_user_func(create_function('$a', 'return call_user_func_array("twoParameters", $a);'), array($value, $pattern));