Php 将create_函数替换为匿名函数

Php 将create_函数替换为匿名函数,php,anonymous-function,php-7.2,create-function,Php,Anonymous Function,Php 7.2,Create Function,在发布问题之前,我已经检查过了 这是使用create\u函数的代码的一小部分 $lambda_functions[$code_hash] = create_function('$action, &$self, $text', 'if ($action == "encrypt") { '.$encrypt.' } else { '.$decrypt.' }'); 我试过用这种方法 $lambda_functions[$code_hash] = function ( $action, &a

在发布问题之前,我已经检查过了

这是使用
create\u函数的代码的一小部分

$lambda_functions[$code_hash] = create_function('$action, &$self, $text', 'if ($action == "encrypt") { '.$encrypt.' } else { '.$decrypt.' }');
我试过用这种方法

$lambda_functions[$code_hash] = function ( $action, &$self, $text ) use ( $encrypt, $decrypt ) {
                if ($action == "encrypt") {
                    return $encrypt;
                } else {
                    return $decrypt;
                }
            };
但不能按预期工作
$encrypt
$decrypt
将包含类似以下内容的代码

$encrypt = $init_encryptBlock . '
           $ciphertext = "";
           $text = $self->_pad($text);
           $plaintext_len = strlen($text);

           $in = $self->encryptIV;

             for ($i = 0; $i < $plaintext_len; $i+= '.$block_size.') {
              $in = substr($text, $i, '.$block_size.') ^ $in;
                        '.$_encryptBlock.'
                        $ciphertext.= $in;
             }

             if ($self->continuousBuffer) {
               $self->encryptIV = $in;
             }

             return $ciphertext;
             ';
$encrypt=$init\u encryptBlock'
$ciphertext=“”;
$text=$self->\u pad($text);
$plaintext_len=strlen($text);
$in=$self->encryptIV;
对于($i=0;$i<$plaintext\u len;$i+='。$block\u size')){
$in=substr($text,$i,“.$block_size.”)^$in;
“.$\u加密块。”
$ciphertext.=$in;
}
如果($self->continuousBuffer){
$self->encryptIV=$in;
}
返回$ciphertext;
';

使用
create_function
可以正常工作,但不使用
anonymous
函数不确定我出了什么问题?

不同的是,使用
create_function()
时,您的代码作为字符串提交并解释为代码,但使用匿名函数时,字符串被解释为字符串,与它包含的代码不同

您可以从
$encrypt
$decrypt
中的字符串中提取代码。这看起来像这样:

/*
 * Removed the "use ($encrypt, $decrypt)" part, 
 * because those were the strings that contained the code, 
 * but now the code itself is part of the anonymous function.
 * 
 * Instead, i added "use ($block_size)", because this is a vairable,
 * which is not defined inside of your function, but still used in it.
 * The other code containing variables might include such variables as
 * well, which you need to provide in the use block, too.
 */
$lambda_functions[$code_hash] = function ( $action, &$self, $text ) use ($block_size) {
    if ($action == "encrypt") {
        //Extract $init_encryptBlock here
        $ciphertext = "";
        $text = $self->_pad($text);
        $plaintext_len = strlen($text);

        $in = $self->encryptIV;

        for ($i = 0; $i < $plaintext_len; $i+= $block_size) {
            $in = substr($text, $i, $block_size) ^ $in;
            // Extract $_encryptBlock here
            $ciphertext.= $in;
        }

        if ($self->continuousBuffer) {
            $self->encryptIV = $in;
        }

         return $ciphertext;
    } else {
        //Extract $decrypt here
    }
};
/*
*删除了“使用($encrypt,$decrypt)”部分,
*因为这些是包含代码的字符串,
*但是现在代码本身是匿名函数的一部分。
* 
*相反,我添加了“use($block_size)”,因为这是一个可值,
*它没有在函数内部定义,但仍在函数中使用。
*包含变量的其他代码可能包括以下变量
*嗯,这也需要在use块中提供。
*/
$lambda_函数[$code_hash]=函数($action,&$self,$text)使用($block_size){
如果($action==“encrypt”){
//在此处提取$init_encryptBlock
$ciphertext=“”;
$text=$self->\u pad($text);
$plaintext_len=strlen($text);
$in=$self->encryptIV;
对于($i=0;$i<$plaintext\u len;$i+=$block\u size){
$in=substr($text,$i,$block_size)^$in;
//在此提取$\u加密块
$ciphertext.=$in;
}
如果($self->continuousBuffer){
$self->encryptIV=$in;
}
返回$ciphertext;
}否则{
//在这里提取$decrypt
}
};

请记住,这不是一个完整的答案。您可以在代码中找到大量的
//Extract$variable here
注释,这些注释代表代码中包含变量的每一个代码,您的代码中包含变量,并且需要按照我从
$encrypt

中提取代码的方式提取这些注释。您是否尝试过检查您创建的lambda代码返回的值?您正在将字符串分配给$encrypt。在将其提交给lambda函数之前,您应该使用eval函数,如@philipp said create_function将字符串评估为php函数。@Christopherayo感谢您的输入,
create_function
的输出为
�lambda_1
我还需要一种不使用
eval
create_函数
的方法,因为这里有警告(@ChristopherPelayo感谢您的输入,
create_函数
的输出是
�lambda_1
我还需要一种不使用
eval
create_函数
的方法,因为感谢您的时间,我将通过添加提取的代码进行检查。