how to use them instead of create_function in PHP 8

PHP rolls out major updates almost every year. In new versions, features appear that allow you to increase productivity, and sometimes the syntax partially changes. With the release of the update, the two previous versions of PHP are supported by the developers for another three years – they continue to fix bugs and improve security. As time passes, older versions of PHP become unmaintained and some features stop working completely. If you do not update the code in the project in time, errors will begin to appear.

Cover by expert Vitaly KireevVitaly Kireev

Head of Research and Development at hosting provider SpaceWeb

My name is Vitaly Kireev, I am the head of the research and development department of the hosting provider SpaceWeb. In this article I will tell you how our team and I refactored the code when moving from PHP 7.4 to PHP 8 and what we replaced one of the most popular functions with – create_function.

The article will also be useful for those who are just diving into the PHP language. Using an example of a real refactoring task, I will analyze in detail how to use anonymous and arrow functions. And I’ll explain how they differ.

Context: when it became clear that refactoring was needed

Some SpaceWeb projects have a lot of legacy code. We support projects from PHP 5.6, which appeared in 2014. We switch to new versions of PHP with some frequency, constantly updating the code.

Leaving the same code in older versions of PHP is a bad idea because at some point it may stop working. They may also have security vulnerabilities. Updated versions of PHP usually include bug fixes and new language features that can improve the performance of websites and applications.

When PHP 7.4 came out, the create_function function, which was often used in our code, became deprecated, that is, it no longer supported or worked. Because of this, errors began to appear in the code. Then it became clear that refactoring was needed.

Static code analyzers – PHP Code Style Fixer and PHPstan – helped us highlight some of the problems in the code. But we checked and updated most of the code manually. First of all, we had to figure out what to replace the popular create_function with. First we started transitioning to an anonymous function and a closure. And after a while, arrow functions were introduced to increase code readability and solve additional problems.

Anonymous functions: how they work and how to apply a closure

The peculiarity of anonymous functions is that they set a scope. Within this area, only those variables that are defined there will work. If we write variables outside the function, the code will throw an error.

In fact, create_function, which we commonly used before PHP 8, is a classic example of using an anonymous function. It was often used to process data in an array. The function contains two arguments – an array of function arguments and the code itself.

create_function(‘$v’, ‘return $v > 1;’)

When this feature became deprecated in PHP 8, we started refactoring the code. Anonymous functions began to look like this:

$filterValue = 20;
$newArray = array_filter($oldArray, function($v) use ($filterValue) { return $v > $filterValue;});

But what if we need to call this function in several different places? Then you can use a closure. This will allow you to call a function using it as a variable.

$filterValue = 20;
$func = function($v) use ($filterValue) { return $v > $filterValue;};
$func(23); // true
$func(19); // false

But what if we need to use variables from the environment, and the values ​​of the variables will change during program execution? Arrow functions will help us here.

Arrow functions: what is the difference from anonymous ones and where is it better to use them

Arrow functions allow you to avoid using use all the time because they automatically capture external variables. This allows you to make the code more concise. Arrow functions are also better suited for working with a large number of variables or with frequent changes of variables in the process.

$inn = $this->getContractInn(); $newArray = array_filter($oldArray, fn($v) => $v === $inn);

In this example, the task was to filter contracts and correlate them with the required Taxpayer Identification Number (TIN). If we were to solve this using anonymous functions, then we would have to write a separate function for each INN. And also use use everywhere.

Using arrow functions we were able to solve the problem faster because they allow access to the entire scope. This reduces the time spent on further code maintenance, and the scope above can be accessed without additional functions.

It’s important to note that arrow functions have a limitation. They can’t be too complex, they have a compact syntax. If we need to place multi-line code, we use anonymous functions.

***

Results

To replace create_function in PHP 8, you can use both anonymous and arrow functions.

Anonymous functions in PHP allow you to create functions that do not have specific names. They work in a given scope and only with those variables that are defined there. The advantage of anonymous functions is that they can contain multi-line code, unlike arrow ones.

Arrow functions allow you to avoid constant use and simplify the syntax. They are also better suited for working with a large number of variables or with frequent changes in the process.

Similar Posts

Leave a Reply

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