Anonymous functions, or closures, are functions that do not have a name. They are created on the fly and can be passed as values to other functions, stored in variables, and even used as arguments when working with arrays and callbacks. Their main purpose is to simplify logic and make code more compact and flexible.
General principles
In PHP, such functions are implemented through a special built-in class called Closure. This means that PHP treats each anonymous function as a full-fledged object that can be worked with like any other class instance.
Anonymous functions are often used when processing arrays, working with regular expressions, and writing callbacks, especially if they are one-time functions that do not need to be reused.
Assigning a variable
An anonymous function can be assigned to a variable. This allows you to call it later, like a regular function, simply by referring to that variable. This approach is often used for logic that needs to be repeated in different parts of an application, but without the need to create a named function.
Inheriting variables from an external scope
A special feature of closures in PHP is their ability to inherit variables from the parent scope. To do this, you need to use a special construct called use.
However, there are limitations:
- Superglobal variables are not inherited.
- Variables with names that match the parameters of the function itself cannot be passed to use.
- You also cannot use $this if the function is not declared in the context of a class or declared as static.
There are two ways to pass values:
- By value — the variable is “captured” at the moment the function is defined.
- By reference — any changes to the variable inside the closure will also affect the external variable.
Execution context and scope
It is important to understand that a closure “sees” variables from the area in which it is defined, not where it is called. This is a fundamental difference from global variables. The scope of a closure is its parent function or class, if it is defined inside a method.
A real-life example is calculating the cost of items in a shopping cart. The closure can use variables declared above (e.g., tax) and perform calculations without needing a global context. This makes the code modular and easy to manage.
Automatic access to $this
If a closure is created inside a class method, PHP automatically “attaches” the current object to it. This means that the $this variable will be available inside the anonymous function, and through it — all the properties and methods of the class. This mechanism greatly simplifies working with data inside an object.
Static closures
PHP also allows you to create static anonymous functions. The main difference is that they do not have access to $this and are not bound to the current object. This behavior is useful if you want to avoid unnecessary links between the closure and the object state, which can be important in libraries, helpers, or under high loads.
If you try to use $this in a static anonymous function, PHP will issue a warning and simply ignore the variable.
In addition, you cannot manually “bind” a static anonymous function to an object, as you can with a regular closure. This is another limitation that ensures code isolation.
Where closures are used
Anonymous functions are used everywhere:
- In array processing (map, filter, reduce, etc.).
- In regular expressions, when you need to process each match.
- When working with ORM or API, where the filtering and processing logic is passed as a callback.
- In templating and dynamic interface construction.
PHP 8 and syntax convenience
Starting with version 8.0, the syntax has become even more convenient. It is now permissible to place a comma after the last variable in use, which improves readability and simplifies automatic code formatting.
Conclusion
Anonymous functions are a powerful tool in the PHP developer’s arsenal. They:
- Simplify code structure,
- Allow flexible control of logic,
- Provide a clean and localized scope,
- Can be bound to an object or, conversely, work in isolation.
Proper use of closures allows you to write modular, readable, and modern code. If you are not yet actively using them in your projects, now is the time to start.