簡體   English   中英

PHP 7.2 Function create_function() 已棄用

[英]PHP 7.2 Function create_function() is deprecated

我在下面的應用程序中使用了create_function()

$callbacks[$delimiter] = create_function('$matches', "return '$delimiter' . strtolower(\$matches[1]);");

但是對於 PHP 7.2.0,不推薦使用create_function()

如何為 PHP 7.2.0 重寫上面的代碼?

您應該能夠使用匿名函數(又名閉包)來調用父作用域$delimiter變量,如下所示:

$callbacks[$delimiter] = function($matches) use ($delimiter) {
    return $delimiter . strtolower($matches[1]);
};

我想貢獻一個我在 Wordpress 主題中找到的非常簡單的案例,並且似乎可以正常工作:

具有以下add_filter語句:

add_filter( 'option_page_capability_' . ot_options_id(), create_function( '$caps', "return '$caps';" ), 999 );

將其替換為:

add_filter( 'option_page_capability_' . ot_options_id(), function($caps) {return $caps;},999);

我們可以看到 function() 的用法,非常典型的函數創建,而不是棄用的 create_function() 來創建函數。 希望能幫助到你。

自動升級

如果有人需要將其代碼中的數十個create_function()案例升級為匿名函數,我將使用一個名為Rector的工具。

它遍歷代碼並將create_function替換為匿名函數 1:1。 它在30 種不同的情況下進行了測試。

安裝

composer require rector/rector --dev

設置

假設您要升級/src目錄中的代碼。

# rector.php
<?php

use Rector\Core\Configuration\Option;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Rector\Php72\Rector\FuncCall\CreateFunctionToAnonymousFunctionRector;

return static function (ContainerConfigurator $containerConfigurator) {
    $parameters = $containerConfigurator->parameters();
    $parameters->set(Option::PATHS, [
        __DIR__ . '/src',
    ]);

    $services = $containerConfigurator->services();
    $services->set(CreateFunctionToAnonymousFunctionRector::class);
};

在您的代碼上運行

# this is set run, it only report what it would change
vendor/bin/rector process --config rector.php --dry-run

# this actually changes the code
vendor/bin/rector process --config rector.php

# the "rector.php" config is loaded by default, so we can drop it
vendor/bin/rector process

編輯:使用 PHP Rector 0.8.x 語法更新 2020-10-31

這個匿名函數數組對我有用,請參見下面的代碼:

// This will be a dynamic name that could 
// be used as a function like "namespace".
$dynamic_name = 'my_dynamic_name';

// Here's some variables that you could use in the scope of
// your dynamic anonymous functions. 
$outerVariable = 'If I need this varible, I can use it';
$outerVariableTwo = 'If I need this varible, I can use it too!';

// Create an array that we can later use and turn into 
// and associative array with our new dynamic anonymous functions.
$dynamicAnonFunctions = [];

// Create the first dynamic function.
$dynamicAnonFunctions[($dynamic_name."_func_one")] = function () use ($outerVariable, $dynamic_name) { 
    echo 'Running: function <b>'.$dynamic_name .'_func_one()</b>';
    echo '<br><br>';
    echo $outerVariable;
    echo '<br><br>';
    echo 'This works :)'; 
    echo '<br><br>';
};

// Create the second dynamic function 
$dynamicAnonFunctions[($dynamic_name."_func_two")] = function () use ($outerVariableTwo, $dynamic_name) { 
    echo '- - - - - - - - - - - - - - - - - - - ';
    echo '<br><br>';
    echo 'Running: function <b>'.$dynamic_name .'_func_two()</b>';
    echo '<br><br>';
    echo $outerVariableTwo;
    echo '<br><br>';
    echo 'This also works :)!'; 
    echo '<br><br>';
};

// Call the functions.
$dynamicAnonFunctions[($dynamic_name."_func_one")]();
$dynamicAnonFunctions[($dynamic_name."_func_two")]();

// Halt execution.
exit();

只需將其復制到您的腳本文件中,您就會看到echo語句的輸出,然后只需將函數重新映射到您自己的意願!

快樂編碼 =)

自 PHP 7.4 起,您可以使用箭頭函數

$callbacks[$delimiter] = fn($matches) => $delimiter . strtolower($matches[1]);

箭頭函數比匿名函數短,並且使用父作用域——因此您可以在不傳入 $delimiter 的情況下引用它。

匿名函數解決方案有效,但如果要返回的表達式在字符串中,我認為應該使用eval

$callbacks[$delimiter] = eval('return function($matches){return '.$delimiter.' . strtolower($matches[1]);};');

接受的答案是正確的方法。 但是,在某些情況下您不能更改代碼(遺留代碼、復雜環境)。 對於那種情況,我寫了一個包:

https://github.com/lombax85/create_function

使用composer require lombax85/create_function安裝它composer require lombax85/create_function

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM