Замените create_function на анонимную функцию

я проверил этот перед публикацией вопроса.

это небольшая часть кода, который использует 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 Функция не уверен, где я иду не так?

0

Решение

Разница в том, что с 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 комментарии в коде, которые обозначают каждую переменную, содержащуюся в вашем коде, и которую нужно извлечь так, как я извлек код из $encrypt,

1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector