I have inherited some old code and need to convert the create_function to an anonymous function. I have done that but since I cannot see the code in the anonymous function I do not know if it equals the code in the former create_function.
Here is the code and my question is: is 'my translation' equal to the 'original code'?
public static function makePhpVdtorFromRegex($regex, $match = TRUE)
{
//original code
$s = 'return '.($match?'':'0 == ').'preg_match(\'/'.addslashes($regex).'/\',$v);';
return create_function('$v', $s);
// my translation
return function($v) use ($regex, $match) {
return ($match?'':'0 == ').preg_match('/'.addslashes($regex).'/',$v);
};
}
I believe makePhpVdtorFromRegex stands for 'Make PHP Validator From Regex'. The problem in validating this is I am not sure where the actual validator is used as this anonymous function is stored in an array which is used to validate input at some later time doing form input validation.
Because $regex and $match only exist within makePhpVdtorFromRegex() they will not be available when the validator is ultimately run, right? So I suspect my translation is not going to work?
useyou'll effectively bind the created function to the values from the outer call (what's called a closure, since you'll "close" over the given variables and their value will be kept from what their value was when the function was created).