Home > Backend Development > PHP Tutorial > Use of php anonymous functions

Use of php anonymous functions

王林
Release: 2023-04-08 09:48:02
forward
3553 people have browsed it

Use of php anonymous functions

The introduction of anonymous functions

Before the emergence of anonymous functions, all functions needed to be named before they could be used

Use of php anonymous functions

Sometimes a function may only need to be used once. In this case, using an anonymous function will make the code more concise and intuitive, and also prevent the function from being used in other places

Use of php anonymous functions

(Free learning video tutorial sharing: php video tutorial)

Definition and use of anonymous functions

PHP will close They are regarded as equivalent concepts to anonymous functions (collectively referred to as anonymous functions in this article). They are essentially objects disguised as functions.

The essence of anonymous functions is objects, so just like objects, anonymous functions can be assigned to a variable

Use of php anonymous functions

All anonymous functions are Closure objects Instance

Use of php anonymous functions

The object has no parent scope to speak of, so you need to use use to manually declare the variables used

Use of php anonymous functions

If you want the variables in the anonymous function to take effect, you need to use reference pass-by-value

Use of php anonymous functions

Starting from PHP 5.4, when using an anonymous function in a class, the $this of the anonymous function will Automatically bind to the current class

Use of php anonymous functions

#If you don’t want automatic binding to take effect, you can use a static anonymous function

Use of php anonymous functions

The essence of anonymous functions

The essence of anonymous functions is the Closure object, including the following five methods

Use of php anonymous functions

__construct - Prevent anonymous functions from being Instantiation

Use of php anonymous functions

Closure::bindTo - Copies the current anonymous function object, binding the specified $this object and class scope. In layman's terms, it means manually binding an anonymous function to a specified object. Using this, you can extend the functionality of the object.

Use of php anonymous functions

Closure::bind - A static version of the bindTo method, there are two usages:

Usage 1: To achieve the same effect as the bindTo method

Use of php anonymous functions

Usage 2: Bind the anonymous function to the class (rather than the object). Remember to set the second parameter to null

Use of php anonymous functions

call - The new call method in PHP 7 can bind and call anonymous functions. In addition to simpler syntax, the performance is also higher

Use of php anonymous functions

fromCallable - will give A certain callable function is converted into an anonymous function

Use of php anonymous functions

##fromCallable is equivalent to

Use of php anonymous functions

There is one thing that needs special attention here. , whether it is a closure converted fromCallable or a closure obtained using reflection, when using bindTo, if the second parameter specifies the binding class, an error will be reported

Use of php anonymous functions

Recommended related article tutorials: php tutorial

The above is the detailed content of Use of php anonymous functions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template