Home > Backend Development > PHP Tutorial > Anonymous functions and closure usage in php

Anonymous functions and closure usage in php

墨辰丷
Release: 2023-03-25 20:24:01
Original
1452 people have browsed it

This article mainly introduces the usage of anonymous functions and closures in php. Interested friends can refer to it. I hope it will be helpful to everyone.

1: Anonymous functions (can only be used in php5.3.0 or above)

Anonymous functions (Anonymous functions) in php, also called Closure function (closures) allows specifying a function without a name. The most commonly used one is the parameter value of the callback function. (http://php.net/manual/zh/functions.anonymous.php)

Definition of anonymous function:

$closureFunc = function(){

    ....

 };
Copy after login

eg : Assign the anonymous function to a variable and call it through the variable



 $closureFunc = function($str){

   echo $str;

  };  $closureFunc("hello world!");
Copy after login


Output: hello world !

2: Closure

2.1 Put the anonymous function in the ordinary function, you can also put the anonymous function Return, this constitutes a simple closure



function closureFunc1(){    $func = function(){        echo "hello";
    };    $func();
}
closureFunc1();//输出: hello
Copy after login


##2.2 Reference local variables in anonymous functions


##

function closureFunc2(){    $num = 1;    $func = function(){        echo $num;
    };    $func();
}
closureFunc2();//Notice: Undefined variable: num
Copy after login


After the above function is run, it will A Notice error is reported, indicating that we cannot use local variables in anonymous functions. At this time, we need to quote a PHP keyword use. The code is as follows



function closureFunc2(){    $num = 1;    $func = function() use($num){        echo $num;
    };    $func();
}
closureFunc2();//输出: 1
Copy after login


 2.3  返回匿名函数



function closureFunc3(){    $num = 1;    $func = function() use($num){        echo $num;
    };    return $func;
}$func = closureFunc3(); //函数返回匿名函数$func(); //然后我们在用$func() 调用//输出: 1
Copy after login


2.4 当我们在返回匿名函数时候怎么给匿名函数传参呢? 其实跟普通函数传参数一样



function closureFunc4(){    $num = 1;    $func = function($str) use($num){        echo $num;        echo "\n";        echo $str;
    };    return $func;
}$func = closureFunc4();$func("hello, closure4");//输出:
//1
//hello, closure4
Copy after login


2.5 怎么用闭包来改变上下文引用的变量值?



function closureFunc5(){    $num = 1;    $func = function() use($num) {        echo "\n";        $num++;        echo $num;
    };    echo "\n";    echo $num;    return $func;
}$func = closureFunc5();$func();$func();$func();//输出:
// 1
// 2
// 2
// 2
Copy after login

Anonymous functions and closure usage in php

看上面的输入结果,显然没有达到目的, 其实只要加一个 & 引用符号就可以了



function closureFunc5(){    $num = 2;    $func = function() use(&$num) {        echo "\n";        $num++;        echo $num;
    };    echo "\n";    echo $num;    return $func;
}$func = closureFunc5();$func();$func();$func();//输出:
// 2
// 3
// 4
// 5
Copy after login


2.6 把匿名函数当作参数传递



function callFunc($func){    $func("argv");
}

callFunc(function($str){    echo $str;
})//输出:
// argv
Copy after login

相关推荐:

关于JavaScript 闭包的相关知识的了解和使用

js闭包使用详解

关于Python闭包机制的深入理解

The above is the detailed content of Anonymous functions and closure usage in php. 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