Forum Moderators: coopster

Message Too Old, No Replies

Variable Functions

Can a variable be declared with a default function argument?

         

Vali

10:16 pm on Feb 28, 2008 (gmt 0)

10+ Year Member



Hey

I had this idea about form validation using variable Functions, but with a twist.

I need to do something like this:


<?php
function foo($arg = '')
{
echo "In foo(); argument was '$arg'.<br />\n";
}
$func = 'foo';
$func('test'); // This calls foo() with the argument 'test'
?>

(That code works)

But where I pass in a default argument when I declare my variable, something like this:


<?php
function foo($arg1 ='', $arg2 = '')
{
echo "In foo(); argument 1 was '$arg1' and argument 2 was '$arg2'.<br />\n";
}
$func = 'foo("my first argument")'; // This part DOES not work
$func('test'); // This calls foo() with the first argument "my first argument" and second argument 'test'
?>

(this code does not work)

The idea being that create an array to validate my input, where the array keys are the field names, and the value is the function to call to validate that particular field value.

So I don't have to create one function for each field, I want to have one function to validate using a regular expression, that I type in at design time (in that validation array).

Can this be done with variable functions? If so how?

[edited by: Vali at 10:17 pm (utc) on Feb. 28, 2008]

coopster

4:17 pm on Feb 29, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



No, you cannot do it that way and maybe this explanation will help you understand why. You have this statement to define your variable as a function name:
$func = 'foo("my first argument")'; // This part DOES not work

But that is not a valid function name. If it were, you would be able to create a function by that name, like this ...
function foo("my first argument")($arg1 = '', $arg2 = '') {

That just won't work.

I think I understand where you may be headed with this, but can you show a bit more information such as how your intended array may be structured?

Vali

4:39 pm on Feb 29, 2008 (gmt 0)

10+ Year Member



Hey

Well, thanks for the reply, but I know that the line does not work :p
It was just meant to show what I needed (add the function name + a parameter to that variable, in one string)

My array would look like this (note, does not work, it's just for the sake of giving an example of the idea):


$validationData[fomnField1] = "normal_regex('/^[1-9]+$/')";
$validationData[fomnField2] = "normal_regex('/^[a-z]+$/')";
$validationData[fomnField3] = "special_validation";
$validationData[fomnField4] = 1;

The idea was to validate the fields like this:


foreach ($_POST as $key => $value) {
.. if (issset($validationData[$key])) {
.... if (!validationData[$key]($value)) {
...... $error .= "You have an error with the $key field.<br />";
.... }
.. }
}

So far, the only way I found of doing it, is to make the $validationData array be a multidimensional array, that will have the function name in index 0, and the parameters after that.

But that makes my simple validation code a bit bigger, slower, and the config file with the validation rules gets a bit more complex.

I was looking for a simpler way to validate the user input.
Any ideas?

coopster

4:59 pm on Feb 29, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



but I know that the line does not work

I just copied/pasted your code. I was explaining why it did not work, for clarification for you and also for future readers.

Have you considered passing the entire $validation array to the class (or function) and loop and process it within there rather than loop through and pass arguments to the class/function for processing?

PHP_Chimp

10:44 pm on Mar 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I use a similar thing to store a whole load of regexes that are used for different validations. Mine is used in a class, so is just another function that returns the pattern. So you should be able to use something like -

$test = array(
'text' => '%[a-z\s\.,\?]+%i',
'number' => '%[0-9]+%'
);
function validate($input, $type) {
global $test;
if(preg_match($test[$type], $input)) {
$out = 'This is ok';
}
else {
$out = 'This is not ok';
}
}

I think this is what you are getting at trying to achieve, although this is untested code.

Vali

4:30 pm on Mar 3, 2008 (gmt 0)

10+ Year Member



PHP_Chimp: my idea is similar, except that I sometimes call a function that uses regular expression + a few other things to validate the data.

Example:
- User name (must be unique)