function exists problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

function exists problem

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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?
programmermatt
Forum Commoner
Posts: 65
Joined: Tue Mar 15, 2005 5:03 pm
Contact:

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Thats what I get when writting code in XXX hours straight :0

Thanks dude
programmermatt
Forum Commoner
Posts: 65
Joined: Tue Mar 15, 2005 5:03 pm
Contact:

Post by programmermatt »

Jcart wrote:Thats what I get when writting code in XXX hours straight :0

Thanks dude
No problem
Post Reply