简体   繁体   English

PHP 将 create_function 更改为匿名 function

[英]PHP change create_function to anonymous function

I have inherited some old code and need to convert the create_function to an anonymous function.我继承了一些旧代码,需要将 create_function 转换为匿名 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.我已经这样做了,但是由于我看不到匿名 function 中的代码,所以我不知道它是否等于前一个 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'.我相信 makePhpVdtorFromRegex 代表'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.验证这一点的问题是我不确定实际验证器在哪里使用,因为这个匿名 function 存储在一个数组中,该数组用于在稍后进行表单输入验证时验证输入。

Because $regex and $match only exist within makePhpVdtorFromRegex() they will not be available when the validator is ultimately run, right?因为 $regex 和 $match 仅存在于 makePhpVdtorFromRegex() 中,所以在验证器最终运行时它们将不可用,对吧? So I suspect my translation is not going to work?所以我怀疑我的翻译不起作用?

To mimic the original behaviour, you should be able to replace it with (for testing purposes, I turned the method into a function):为了模仿原始行为,您应该能够将其替换为(出于测试目的,我将方法转换为函数):

function makePhpVdtorFromRegex($regex, $match = true) {

    if ($match) {

        return function($value) use ($regex) {

            return preg_match('/'.addslashes($regex).'/', $value);
        };
    }
    
    return function($value) use ($regex) {
        
        // Same as '0 == preg_match(...)' from original code
        return !preg_match('/'.addslashes($regex).'/', $value);
    };
}

$validator = makePhpVdtorFromRegex('^[a-z]+$');

// Check if something matches

var_dump($validator('abc')); // true

// Check if something doesn't match
 
$validator = makePhpVdtorFromRegex('^[a-z]+$', false);

var_dump($validator('123')); // true

If you've the chance to look into the actual form validation later on & maybe even take control of the regular expressions themselves, you could rewrite this code to something much simpler, like:如果您稍后有机会研究实际的表单验证,甚至可以控制正则表达式本身,您可以将此代码重写为更简单的代码,例如:

function getRegexValidator() {

    return function($regex, $value) {

        return preg_match($regex, $value);
    };
}

$validator = getRegexValidator();

// Check if something matches

var_dump($validator('/^[a-z]+$/', 'abc')); // true

// Check if something doesn't match

var_dump(!$validator('/^[a-z]+$/', '123')); // true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM