How to use anonymous function php - php

I have an error with phpstorm when I want to change this function
$callback = create_function('$matches', 'return strtoupper($matches[1]);');
by
callback = function('$matches', 'return strtoupper($matches[1]);');
How to resolve that if it's an error.
Thank you.

You shouldn't use create_function(). create_function() uses eval(). eval() is evil.
On a more serious note, eval() (and thus create_function()) has big security issues. If you're on PHP 5.3 or higher, you should use native anonymous functions instead, in this case:
$callback = function($matches) {
return strtoupper($matches[1]);
}
For reference: Anonymous functions.
Note that create_function has been deprecated as of PHP 7.2.

Related

How do I convert this `create_function` call to an anonymous function?

I've got some code that uses create_function, which is now deprecated. The comments for create_function say * #deprecated 7.2 Use anonymous functions instead.
This is the function itself: create_function ('$matches', 'return strtoupper("\0");')
And this is the context:
$match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./',
create_function ('$matches', 'return strtoupper("\0");'), strtolower(trim($match[1])));
My code isn't running because it's been deprecated, so I want to turn it into an anonymous function instead. I'm not entirely sure what this bit of code does, so I don't really have a reliable way of testing it, but I THINK that create_function block can be replaced with
function($matches) {
return strtoupper($matches);
}
Would that work? Does that do the same thing that the create_function function does? I'm not sure what "\0" does in that context, so I'm hoping I can get some advice.
I found a similar question another place and tried the answer there. Turns out it's equivalent to this function:
function($m) {
return strtoupper($m[0]);
}
Found it here: Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback
thanks to #Philipp

Deprecated preg_replace in 5.5.3

I am currently re-programming a framework in php and I upgraded our development server to php 5.5.3. When I fire up the webbrowser, it returns the following error:
[19-Oct-2013 16:54:05 Europe/Amsterdam] PHP Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /Applications/MAMP/htdocs/fw/lib/lang.php on line 57
And line 57 is;
$response = preg_replace('/\{'.$find.'(:(.*))?\}/Ue', 'str_replace("{value}", "\\2", $replace)', $response);
I am really horrible at reading these php documentation, I am a beginner and simply changing preg_replace() by preg_replace_callback() is too good to be true. A co-worker told me it had to be something like $value[1] but that didn't work eather.
Is there a simple solution, am I overlooking something?
Here is the page about modifiers, giving you more detail about why it is deprecated, and what that means exactly.
Basically the reason is that the /e modifier causes a string to be evaluated as PHP code, as if eval was called. Using preg_replace_callback instead, which allows you to pass it an actual PHP function is indeed the way to go.
If you replace the string-code (second parameter) by an anonymous function, your code should then look something like this:
$response = preg_replace_callback(
'/\{'.$find.'(:(.*))?\}/U',
function($m) use ($replace) {
return str_replace($m, "\\2", $replace);
} ,
$response);
The keyword use makes sure that the anonymous function can use the $replace variable which should be defined in the calling scope. See this discussion.

What's the syntax to get a create_function function to work as a callback:

I attempted the following code:
$for_callback=create_function('$match','return $GLOBALS[\'replacements\'][$match[1]];');
$result = preg_replace_callback( $regex, '$for_callback', $string);
The variable $GLOBALS['replacements'] is generated dynamically before this function is called.
I get an error message like
Warning: preg_replace_callback() [function.preg-replace-callback]: Requires argument 2, '$for_callback', to be a valid callback in...
created functions and callbacks are both new to me. This is growing out of some code given to me by nickb at My question about preg_replace that turned into preg_replace_callback.
What I'm trying to do is wrap the code in that answer into a function and I'm running into errors with scope avoiding re-defining a function. (upgrading to PHP 5.3+ is a remote posibility option for me at the moment.)
How do I get this to work?
First, variables must not be enclosed with single quotes, as they will not be replaced with the real value.
And second, you should use anonymous functions (i.e. closures) instead as they are much easier. Use them like in this example:
$for_callback = function($match) {
return $GLOBALS['replacements'][$match[1]];
};
$result = preg_replace_callback( $regex, $for_callback, $string);
edit: Closures became available in PHP 5.3. So if you are still using PHP < 5.3 you should (really update or) use the following:
$for_callback=create_function('$match','return $GLOBALS[\'replacements\'][$match[1]];');
$result = preg_replace_callback( $regex, $for_callback, $string);

Which version of php added anonymous functions

In manual there is create_function function and you can pass result from that function to array_map, I thought that that is the only way to have something like anonymous functions and closures, but then I found that I can just put function like in javascript
array_map(function($a) {
return $a + 1;
}, array(1, 2, 3, 4, 5));
In which version of php I can do this? Was this always there?
Closures (anonymous functions) were added in PHP 5.3.0, including the use clause.
Then since PHP 5.4.0 the static keyword is supported in front of it to denote a static function.
And as of PHP 7.4.0 arrow functions (RFC) as a more concise syntax.
Anonymous functions are available since PHP 5.3:
The key features of PHP 5.3.0 include:
…
Lambda Functions and Closures
…
PHP >5.3:
http://php.net/manual/en/functions.anonymous.php

"Function split() is deprecated" in PHP?

$stringText = "[TEST-1] test task 1 Created: 06/Apr/11 Updated: 06/Apr/11";
$splitArray = split(" ",$stringText);
Deprecated: Function split() is deprecated in C:\wamp\www\RSS.php on line 27
Why this error happen ?
http://php.net/manual/en/function.split.php
From the manual
Warning This function has been
DEPRECATED as of PHP 5.3.0. Relying on
this feature is highly discouraged
Note:
As of PHP 5.3.0, the regex extension
is deprecated in favor of the PCRE
extension. Calling this function will
issue an E_DEPRECATED notice. See the
list of differences for help on
converting to PCRE.
I guess you're supposed to use the alternative preg_split(). Or if you're not using a regex, just use explode
split has been replaced with explode, see http://php.net/explode for more information. Works the same as split, but split is 'deprecated' basically means that is a old function that shouldn't be used anymore, and is not likely to be in later versions of php.
Use following explode function:
$command = explode(" ", $tag[1]);
This is the standard solution for this case.
Its Perfectly working.
Ahh, the docs says about it. And the docs also say which functions should be used instead of this:
preg_split
explode
str_split
Because the function has been deprecated? You can customize the error_reporting level to not log / display the depreciated errors. But it would be more prudent to just correct the issue (IE use explode instead for the simple split you are doing above.)
You can use this custom function for old codes:
if (!function_exists('split')) {
function split($pattern, $subject, $limit=-1, $flags=0){
return preg_split($pattern, $subject, $limit, $flags);
}
}

Categories