【问题标题】:Replace create_function with anonymous function用匿名函数替换 create_function
【发布时间】:2018-04-27 08:46:22
【问题描述】:

我在发布问题之前检查了this

这是使用create_function的代码的一小部分

$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;
             ';

create_function 工作正常,但anonymous 函数不知道我哪里出错了?

【问题讨论】:

  • 您是否尝试过检查您创建的 lambda 代码返回的值?您正在将字符串分配给 $encrypt 您应该在将其提交给 lambda 函数之前使用 eval 函数,例如 @philipp 说 create_function 正在评估字符串作为 php 函数。
  • @ChristopherPelayo 感谢您的输入,create_function 的输出是�lambda_1 我也需要一种不使用evalcreate_function 的方法,因为小心[这里](@ChristopherPelayo感谢您的输入,create_function 的输出是�lambda_1 我也需要一种不使用evalcreate_function '因为caution 的方法

标签: php anonymous-function php-7.2 create-function


【解决方案1】:

不同之处在于,对于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
    }
};

请记住,这不是一个完整的答案。您会在代码中找到许多 // Extract $variable here cmets,它们代表包含变量的每个代码,您在代码中拥有这些变量,并且需要顺便提取这些变量,其中我从 $encrypt 提取代码。

【讨论】:

  • 感谢您抽出宝贵时间,我将通过添加提取的代码进行检查。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多