PHP 7.2 函数 create_function() 已弃用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48161526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP 7.2 Function create_function() is deprecated
提问by Saly
I have used create_function in my application below.
我在下面的应用程序中使用了 create_function。
$callbacks[$delimiter] = create_function('$matches', "return '$delimiter' . strtolower($matches[1]);");
But for PHP 7.2.0, the create_function() is deprecated.
但是对于 PHP 7.2.0,不推荐使用 create_function()。
Any idea, how to fix my codes above on PHP 7.2.0.
任何想法,如何在 PHP 7.2.0 上修复我上面的代码。
Thanks for your help,
谢谢你的帮助,
回答by e_i_pi
You should be able to use an Anonymous Function(aka Closure) with a call to the parent scoped $delimiter
variable, like so:
您应该能够通过调用父作用域变量来使用匿名函数(又名闭包)$delimiter
,如下所示:
$callbacks[$delimiter] = function($matches) use ($delimiter) {
return $delimiter . strtolower($matches[1]);
};
回答by Joanmacat
I would like to contribute with a very simple case I found in a Wordpress Theme and seems to work properly:
我想用我在 Wordpress 主题中找到的一个非常简单的案例做出贡献,并且似乎可以正常工作:
Having the following add_filterstatement:
具有以下add_filter语句:
add_filter( 'option_page_capability_' . ot_options_id(), create_function( '$caps', "return '$caps';" ), 999 );
Replace it for:
将其替换为:
add_filter( 'option_page_capability_' . ot_options_id(), function($caps) {return $caps;},999);
We can see the usage of function(), very typical function creation instead of a deprecated create_function() to create functions. Hope it helps.
我们可以看到 function() 的用法,非常典型的函数创建而不是不推荐使用的 create_function() 来创建函数。希望能帮助到你。
回答by Tomá? Votruba
If anyone needs to upgrade dozens of create_function()
cases in their code to anonymous functions, I work on a tool called Rector.
如果有人需要将create_function()
代码中的数十个案例升级为匿名函数,我会使用一个名为Rector的工具。
It goes through the code and replaces the create_function
with anonymous functions 1:1. It's tested on 30 various cases.
它遍历代码并create_function
用匿名函数 1:1替换。它在30 种不同的情况下进行了测试。
Install
安装
composer require rector/rector --dev
Setup
设置
# rector.yml
services:
Rector\Php72\Rector\FuncCall\CreateFunctionToAnonymousFunctionRector: null
Run on your code
运行你的代码
vendor/bin/rector process src --config rector.yml --dry-run
vendor/bin/rector process src --config rector.yml
回答by White Rabbit
This Array of Anonymous functions worked for me, see code below:
这个匿名函数数组对我有用,见下面的代码:
// 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();
Just copy this into your script file and you will see the output from the echo
statements, then simply remap the function to your own will!
只需将其复制到您的脚本文件中,您就会看到echo
语句的输出,然后只需将函数重新映射到您自己的意愿即可!
Happy coding =)
快乐编码 =)