Home > Backend Development > PHP Tutorial > Detailed explanation of the implementation of anonymous functions and closures in php functions

Detailed explanation of the implementation of anonymous functions and closures in php functions

伊谢尔伦
Release: 2023-03-07 19:12:01
Original
2944 people have browsed it

php supports callback functions (callback), which is a feature that adds more points compared with other high-level languages. However, compared with JavaScript, the use of callback functions before php5.3 is not very flexible. There are only two options: the function name of string and the return value of create_function . After php5.3, we have another option--Anonymous function, also called closure function(closure), which allows to temporarily create an unspecified The name of the function, often used as the value of the callback function parameter. Of course, there are other applications as well. In the previous chapter, we introduced the detailed usage of php callback function. This chapter will talk about PHP's anonymous or closure custom function.

Here we will take an example of an anonymous or closure custom function, declare a custom function, and assign an unnamed function to a variable. Add parentheses directly after the variable and pass the parameters to call the anonymous function and output it.

The sample code of the anonymous function is as follows:

<?php
/** 匿名或者闭包函数实例 */
 $fun = function($a){
   echo $a;             //将一个没有名字的函数赋值给一个变量$fun
 };
 $fun("php.cn");        //变量后加括号并传参数,调出匿名函数并输出
?>
Copy after login

Anonymous functions can also be used as the value of variables. Directly passing the anonymous function as a parameter to the callback function is the most common use of anonymous functions. In the end, you forget to add a semicolon.

A code example that takes an anonymous function as a parameter when calling a callback function is as follows:

<?php
/** 申明函数callback,需要传递一个匿名函数作为参数 */
function callback($back){
 echo $back();             //参数只有是一个函数时才能在这里调用
}
callback(function(){        //调用函数的同时直接传入一个匿名函数
 echo "闭包数据";
});
?>
Copy after login

An important concept of closures is that external variables can be used in internal functions , you need to use the keyword use to connect the closure function and external variables. These variables must be declared at the head of the function or class. Closure functions inherit variables from the parent scope, which is different from using global variables. Global variables exist in a full scope, regardless of which function is currently executing. The parent scope of the closure defines the closure function, not necessarily the function that calls it.

Below we also illustrate through an example of a closure, declaring a function and passing an anonymous function as a parameter. The parameter here can only be called when it is a function. The closure uses the use keyword to use external variables in the internal function. . Pass in the anonymous function directly when calling the function.

The code for using the keyword use is as follows:

<?php
/** 申明函数callback,需要传递一个匿名函数作为参数 */
function callback($back){
 echo $back();             //参数只有是一个函数时才能在这里调用
}
$var = "测试数据";    //闭包的一个重要概念就是内部函数中使用外部变量,通过 use 关键字实现
callback(function() use(&$var){        //调用函数的同时直接传入一个匿名函数
 echo "闭包数据的:{$var}";
});
?>
Copy after login

Note: In the above example, the variable referenced by use is a copy of $var. If you want to fully quote , you need to add & symbols like the above example.

【Related tutorial recommendations】

1. "php.cn Dugu Jiujian (4)-php video tutorial"

2. Video tutorial :The implementation of anonymous functions and closures: the principles of function expressions and closures

3. php practical video tutorial

The above is the detailed content of Detailed explanation of the implementation of anonymous functions and closures in php functions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template