Dynamic Function Call In A Class

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
aspekt9
Forum Commoner
Posts: 43
Joined: Wed Dec 06, 2006 5:03 pm

Dynamic Function Call In A Class

Post by aspekt9 »

I have a class and I'm trying to call a function from outside of it that calls a dynamic function inside the class using call_user_func_array(). I think it's a problem with call_user_func_array not checking the local functions first before looking globally because if I put the functions in the file I call validate_data from it works. How can I get around this? Is there another function I can use to call dynamic functions? To make more sense here's my code and the errors I get:

Code: Select all

 
    function validate_data($data, $val_array) {
        
        $error = array();
        
        foreach ($val_array as $var => $val_seq)
        {
            if (!is_array($val_seq[0]))
            {
                $val_seq = array($val_seq);
            }
 
            foreach ($val_seq as $validate)
            {
                $function = array_shift($validate);
                array_unshift($validate, $data[$var]);
 
                if ($result = call_user_func_array('validate_' . $function, $validate))
                {
                    $error[] = strtoupper($var) . '_' . $result;
                }
            }
        }       
        return $error;
    }
I use this line to call validate data:

Code: Select all

$formValidation->validate_data($data, $val_array);

The errors I get are:

Code: Select all

 
Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'validate_string' was given in C:\Apache\htdocs\toss\classes\formvalidation.class.php on line 45
 
Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'validate_username' was given in C:\Apache\htdocs\toss\classes\formvalidation.class.php on line 45
 
Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'validate_string' was given in C:\Apache\htdocs\toss\classes\formvalidation.class.php on line 45
 
Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'validate_password' was given in C:\Apache\htdocs\toss\classes\formvalidation.class.php on line 45
 
Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'validate_string' was given in C:\Apache\htdocs\toss\classes\formvalidation.class.php on line 45
 
Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'validate_string' was given in C:\Apache\htdocs\toss\classes\formvalidation.class.php on line 45
 
Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'validate_string' was given in C:\Apache\htdocs\toss\classes\formvalidation.class.php on line 45
 
Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'validate_numeric' was given in C:\Apache\htdocs\toss\classes\formvalidation.class.php on line 45
 
Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'validate_numeric' was given in C:\Apache\htdocs\toss\classes\formvalidation.class.php on line 45
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Dynamic Function Call In A Class

Post by Christopher »

Where are the functions validate_string(), validate_password(), validate_numeric() declared? They need to be included before this code is run.
(#10850)
Post Reply