Page 1 of 1

function exists problem

Posted: Wed Jun 29, 2005 8:15 pm
by John Cartwright
Well I'm writting an extension to the validation class posted in the code
snipplet forum and basically my class will accept 2 paramaters.

Code: Select all

function checkerrors($type,$required)
$type will be the request method, so $_GET, $_POST, $_SESSION, whatever.
THe required variable is a set of pre-defined variables that should exist, and also their validation type is passed to the function.

Code: Select all

$accepted_post = array(	'responseURL' => 'isWebAddress', 'responseFormat' => 'isInt');
		
if ($builderr->checkerrors($_POST,$accepted_post)) {
If you take a moment and look at the validation class in the code snipplet forums the functions look like isWebAddress, isAllLetters, isInt and so on. I then take in these paramaters and double check the function exists before utalizing it.

Code: Select all

if (function_exists(formValidator::$required[$varname]($value))) {
	$valid = formValidator::$required[$varname]($value);
}
else {
	trigger_error('Invalid Validation Format: formValidator::'.$required[$varname].'()',E_USER_ERROR);
}
I can access the static class's function fine if I call it normally, but when I
run this code to see if the function exists I get an error like the following
Fatal error: Invalid Validation Format: formValidator::isInt() in /home/jcart/httpdoc/class/class.builderrors.php on line 25
Why can't I call the class function this way?

Posted: Thu Jun 30, 2005 12:10 pm
by John Cartwright
Seems that it was just failing at the function_exists part, but would still work if I removed those lines. I generally still would like to perform the check that the function exists.. any thoughts?

Posted: Thu Jun 30, 2005 12:24 pm
by programmermatt

Code: Select all

if (function_exists("formValidator::$required[$varname]")) {
    $valid = call_user_func("formValidator::$required[$varname]", $value);
}
else {
    trigger_error('Invalid Validation Format: formValidator::'.$required[$varname].'()',E_USER_ERROR);
}
That should work. In my understanding function_exists takes a string of the function name. It is not possible to validate for paramaters passed to the function, just that the function exists. as for the $valid part, I think what you want is the result of the function, right? Well then you have to us call_user_func to handle strings.

http://us3.php.net/function_exists
http://us4.php.net/call_user_func

Posted: Thu Jun 30, 2005 12:38 pm
by John Cartwright
Thats what I get when writting code in XXX hours straight :0

Thanks dude

Posted: Thu Jun 30, 2005 1:15 pm
by programmermatt
Jcart wrote:Thats what I get when writting code in XXX hours straight :0

Thanks dude
No problem