Function create_function() PHP Deprecated PHP 7.4 [duplicate] - php

I have used create_function() in my application below.
$callbacks[$delimiter] = create_function('$matches', "return '$delimiter' . strtolower(\$matches[1]);");
But for PHP 7.2.0, create_function() is deprecated.
How do I rewrite my code above for PHP 7.2.0?

You should be able to use an Anonymous Function (aka Closure) with a call to the parent scoped $delimiter variable, like so:
$callbacks[$delimiter] = function($matches) use ($delimiter) {
return $delimiter . strtolower($matches[1]);
};

I would like to contribute with a very simple case I found in a Wordpress Theme and seems to work properly:
Having the following add_filter statement:
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.

Automated Upgrade
If anyone needs to upgrade dozens of create_function() cases in their code to anonymous functions, I work on a tool called Rector.
It goes through the code and replaces the create_function with anonymous functions 1:1. It's tested on 30 various cases.
Install
composer require rector/rector --dev
Setup
Let's say you want to upgrade code in the /src directory.
# 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);
};
Run on your code
# 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
EDIT: Updated 2020-10-31 with PHP Rector 0.8.x syntax

Since PHP 7.4 you can use an Arrow function:
$callbacks[$delimiter] = fn($matches) => $delimiter . strtolower($matches[1]);
Arrow functions are shorter than anonymous functions, and use the parent scope - so you can refer to $delimiter without passing it in.

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!
Happy coding =)

The anonymous function solution works, but if the expression to be returned is in a string I think eval should be used.
$callbacks[$delimiter] = eval('return function($matches){return '.$delimiter.' . strtolower($matches[1]);};');

Related

How can I rewrite a line in a WordPress plugin that contains the deprecated 'create_function' method to maintain PHP 8.0 compatibility? [duplicate]

I have used create_function() in my application below.
$callbacks[$delimiter] = create_function('$matches', "return '$delimiter' . strtolower(\$matches[1]);");
But for PHP 7.2.0, create_function() is deprecated.
How do I rewrite my code above for PHP 7.2.0?
You should be able to use an Anonymous Function (aka Closure) with a call to the parent scoped $delimiter variable, like so:
$callbacks[$delimiter] = function($matches) use ($delimiter) {
return $delimiter . strtolower($matches[1]);
};
I would like to contribute with a very simple case I found in a Wordpress Theme and seems to work properly:
Having the following add_filter statement:
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.
Automated Upgrade
If anyone needs to upgrade dozens of create_function() cases in their code to anonymous functions, I work on a tool called Rector.
It goes through the code and replaces the create_function with anonymous functions 1:1. It's tested on 30 various cases.
Install
composer require rector/rector --dev
Setup
Let's say you want to upgrade code in the /src directory.
# 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);
};
Run on your code
# 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
EDIT: Updated 2020-10-31 with PHP Rector 0.8.x syntax
Since PHP 7.4 you can use an Arrow function:
$callbacks[$delimiter] = fn($matches) => $delimiter . strtolower($matches[1]);
Arrow functions are shorter than anonymous functions, and use the parent scope - so you can refer to $delimiter without passing it in.
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!
Happy coding =)
The anonymous function solution works, but if the expression to be returned is in a string I think eval should be used.
$callbacks[$delimiter] = eval('return function($matches){return '.$delimiter.' . strtolower($matches[1]);};');

errors function is deprecated PHP 7.3.28? [duplicate]

I have used create_function() in my application below.
$callbacks[$delimiter] = create_function('$matches', "return '$delimiter' . strtolower(\$matches[1]);");
But for PHP 7.2.0, create_function() is deprecated.
How do I rewrite my code above for PHP 7.2.0?
You should be able to use an Anonymous Function (aka Closure) with a call to the parent scoped $delimiter variable, like so:
$callbacks[$delimiter] = function($matches) use ($delimiter) {
return $delimiter . strtolower($matches[1]);
};
I would like to contribute with a very simple case I found in a Wordpress Theme and seems to work properly:
Having the following add_filter statement:
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.
Automated Upgrade
If anyone needs to upgrade dozens of create_function() cases in their code to anonymous functions, I work on a tool called Rector.
It goes through the code and replaces the create_function with anonymous functions 1:1. It's tested on 30 various cases.
Install
composer require rector/rector --dev
Setup
Let's say you want to upgrade code in the /src directory.
# 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);
};
Run on your code
# 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
EDIT: Updated 2020-10-31 with PHP Rector 0.8.x syntax
Since PHP 7.4 you can use an Arrow function:
$callbacks[$delimiter] = fn($matches) => $delimiter . strtolower($matches[1]);
Arrow functions are shorter than anonymous functions, and use the parent scope - so you can refer to $delimiter without passing it in.
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!
Happy coding =)
The anonymous function solution works, but if the expression to be returned is in a string I think eval should be used.
$callbacks[$delimiter] = eval('return function($matches){return '.$delimiter.' . strtolower($matches[1]);};');

ErrorException in Util.php line 206: Function create_function() is deprecated [duplicate]

I have used create_function() in my application below.
$callbacks[$delimiter] = create_function('$matches', "return '$delimiter' . strtolower(\$matches[1]);");
But for PHP 7.2.0, create_function() is deprecated.
How do I rewrite my code above for PHP 7.2.0?
You should be able to use an Anonymous Function (aka Closure) with a call to the parent scoped $delimiter variable, like so:
$callbacks[$delimiter] = function($matches) use ($delimiter) {
return $delimiter . strtolower($matches[1]);
};
I would like to contribute with a very simple case I found in a Wordpress Theme and seems to work properly:
Having the following add_filter statement:
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.
Automated Upgrade
If anyone needs to upgrade dozens of create_function() cases in their code to anonymous functions, I work on a tool called Rector.
It goes through the code and replaces the create_function with anonymous functions 1:1. It's tested on 30 various cases.
Install
composer require rector/rector --dev
Setup
Let's say you want to upgrade code in the /src directory.
# 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);
};
Run on your code
# 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
EDIT: Updated 2020-10-31 with PHP Rector 0.8.x syntax
Since PHP 7.4 you can use an Arrow function:
$callbacks[$delimiter] = fn($matches) => $delimiter . strtolower($matches[1]);
Arrow functions are shorter than anonymous functions, and use the parent scope - so you can refer to $delimiter without passing it in.
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!
Happy coding =)
The anonymous function solution works, but if the expression to be returned is in a string I think eval should be used.
$callbacks[$delimiter] = eval('return function($matches){return '.$delimiter.' . strtolower($matches[1]);};');

PHP 7.2 Function create_function() is deprecated

I have used create_function() in my application below.
$callbacks[$delimiter] = create_function('$matches', "return '$delimiter' . strtolower(\$matches[1]);");
But for PHP 7.2.0, create_function() is deprecated.
How do I rewrite my code above for PHP 7.2.0?
You should be able to use an Anonymous Function (aka Closure) with a call to the parent scoped $delimiter variable, like so:
$callbacks[$delimiter] = function($matches) use ($delimiter) {
return $delimiter . strtolower($matches[1]);
};
I would like to contribute with a very simple case I found in a Wordpress Theme and seems to work properly:
Having the following add_filter statement:
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.
Automated Upgrade
If anyone needs to upgrade dozens of create_function() cases in their code to anonymous functions, I work on a tool called Rector.
It goes through the code and replaces the create_function with anonymous functions 1:1. It's tested on 30 various cases.
Install
composer require rector/rector --dev
Setup
Let's say you want to upgrade code in the /src directory.
# 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);
};
Run on your code
# 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
EDIT: Updated 2020-10-31 with PHP Rector 0.8.x syntax
Since PHP 7.4 you can use an Arrow function:
$callbacks[$delimiter] = fn($matches) => $delimiter . strtolower($matches[1]);
Arrow functions are shorter than anonymous functions, and use the parent scope - so you can refer to $delimiter without passing it in.
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!
Happy coding =)
The anonymous function solution works, but if the expression to be returned is in a string I think eval should be used.
$callbacks[$delimiter] = eval('return function($matches){return '.$delimiter.' . strtolower($matches[1]);};');

Upgrading from create_function to closures

Struggling to understand closures for a couple of days. Can anybody point me in the right direction? Need to re-write this "create_function" as a lambda.
$section = preg_replace_callback('/{listing_field_([^{}]*?)_caption}/', create_function('$matches', 'global $config,$or_replace_listing_id,$lang;require_once($config[\'basepath\'].\'/include/listing.inc.php\'); return listing_pages::renderSingleListingItem($or_replace_listing_id, $matches[1],\'caption\');'), $section);
You define a closure like so:
$myClosure = function ($args) {
// do something;
};
create_function takes two arguments - the first is an eval'd string of the callable's arguments, the second is the code to execute - so you'd do something like this:
$section = preg_replace_callback(
// Your regex search pattern
'/{listing_field_([^{}]*?)_caption}/',
// Your callback
function ($matches) use ($config, $or_replace_listing_id) {
require_once $config['basepath'] . '/include/listing.inc.php';
return listing_pages::renderSingleListingItem(
$or_replace_listing_id,
$matches[1],
'caption'
);
},
// Your subject
$section
);
Note that I've replaced your global variable calls with importing them via use into the callback instead, and removed $lang because you aren't using it.

Categories