Rewriting anonymous PHP functions

In newer versions of PHP, create_function is an deprecated function. An example will be given of rewriting such a function.

create_function the function is unsafe due to the fact that it calls the function inside eval ().

We will change it using the example of one of the plugins in WordPress

We look at the site, we see there create_function:

if (!empty($ tags)) {
$ post_tags = ''['[‘ . implode(',', array_map(
create_function('$ v', 'return "'". urlencode (substr ($ v, strpos ($ v, '_ ') + 1)). " '";'), $ tags)) . ']'';
} else {
$ post_tags = ''[]'';
}

This:

create_function('$ v', 'return "'". urlencode (substr ($ v, strpos ($ v, '_ ') + 1)). " '";')

Rewrite it

$ ploshadka = function($ v) {
$ strop = strpos($ v, '_');
$ substr = substr($ v, $ strop + one);
return urlencode($ substr);
};

It was

Has become

Hey. You are on my site. I am a developer. Here I share my best practices and knowledge. Ask in the comments if you don’t understand something or write if you have something to add.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *