Home  >  Article  >  Backend Development  >  [PHP Learning] Six tips for using anonymous functions

[PHP Learning] Six tips for using anonymous functions

little bottle
little bottleforward
2019-04-23 14:12:042451browse

This article mainly talks about six tips for using anonymous functions. It has certain learning value. Interested friends can learn it.

I have written an article about the use of closures before (click here to enter). This time I will give an in-depth summary of the in-depth usage and understanding of anonymous functions in PHP:

Anonymous functions in PHP are also called Closure functions allow specifying a function without a name. Assign the anonymous function to a variable and call it through the variable. Here is a simple example:

<?php$anonymousFunc = function($username){    
    echo $username;
  };
  $anonymousFunc("乔峰!");

Tip 1: Put the anonymous function in a normal function, or you can return the anonymous function. : This forms a simple closure

<?php
function closureFunc(){
    $anonymousFunc = function(){
        echo "乔峰!";
    };
    $anonymousFunc();//普通函数内部调用了匿名函数
}
closureFunc();//输出: 乔峰

Tip 2 Reference local variables in anonymous functions (here you need to quote a php keyword use)

<?php

function closureFunc(){
    $username = &#39;乔峰&#39;;
    $anonymousFunc = function() use($username){
        echo $username;
    };
    $anonymousFunc();//此处调用了匿名函数
}
closureFunc();//输出: 乔峰

Tip 3 Return an anonymous function in a normal function

<?php

function closureFunc(){
    $username = &#39;乔峰&#39;;
    $anonymousFunc = function() use($username){
        echo $username;
    };
    return $anonymousFunc;// 函数返回匿名函数
}
$func = closureFunc();
$func(); //然后调用$func()

Tip 4 Return an anonymous function and pass parameters to the anonymous function

<?php

function closureFunc(){
    $username = &#39;乔峰&#39;;
    $anonymousFunc = function($lover,$skill) use($username){
        echo $username.$lover.$skill;
    };
    return $anonymousFunc;
}
$func = closureFunc();
$func("阿朱","擒龙手");//乔峰阿朱擒龙手

Tips 5 Use closure to change the variable value referenced by the context

<?php

function closureFunc(){
    $number = 100;
    $anonymousFunc = function() use($number) {
        $number++;
        echo $number.PHP_EOL;
    };
    echo $number.PHP_EOL;
    return $anonymousFunc;
}
$func = closureFunc();// 这里输出1,直接调用本函数的 echo $number.PHP_EOL; 即为100
$func();// 调用函数的返回值 $anonymousFunc  $number++ 即为101
$func(); //101
$func();//101

Based on the above input results, it is found that the following two func() both return 101 and the value has not changed. If you want to accumulate the effect, just add An & reference symbol is enough (modifications within the anonymous function will also affect external variables), modify it:

<?php

function closureFunc(){
    $number = 100;
    $anonymousFunc = function() use(&$number) {
        $number++;
        echo $number.PHP_EOL;
    };
    echo $number.PHP_EOL;
    return $anonymousFunc;
}
$func = closureFunc();// 这里输出1,直接调用本函数的 echo $number.PHP_EOL; 即为100
$func();// 调用函数的返回值 $anonymousFunc  $number++ 即为101
$func(); //102
$func();//103

Tip 6 Pass the anonymous function as a parameter

<?php
//定义普通函数,anonymousFunc 为参数变量
function myFunc($anonymousFunc){
    $anonymousFunc("乔峰");
}

myFunc(function($username){ //这里调用普通函数,并把 匿名函数作为参数 传给了myFunc中的$anonymousFunc
    echo $username;
});//输出 乔峰

Related tutorials: PHP video tutorial

The above is the detailed content of [PHP Learning] Six tips for using anonymous functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete