.net 匿名函数_PHP 7.x — P39:匿名函数

.net 匿名函数

Anonymous functions, or closures, are functions that seem to cause a lot of stress in people’s lives. Let’s simplify this process. An anonymous function is a function without a name.

匿名函数或闭包是似乎给人们的生活带来很大压力的函数。 让我们简化这个过程。 匿名函数是没有名称的函数。

Let’s look at a regular function. A regular function starts with the function keyword and is followed by the function name; the parameters are enclosed inside of parentheses and the function body is enclosed inside of curly braces.

让我们来看一个常规函数。 常规函数以function关键字开头,后跟函数名称; 参数括在括号内,函数主体括在花括号内。

The anonymous function drops the function name and assigns the function to a variable.

匿名函数删除函数名称,并将函数分配给变量。

Let’s create an actual example. We’ll show both a standard function and an anonymous function declaration. Let’s start with the regular function. The function is called stuff_dogs_say(). It will just echo out “Dog says Cats Suck” after its been called. We’ve already looked at how to call regular functions in the user defined functions article.

让我们创建一个实际的示例。 我们将同时显示标准函数和匿名函数声明。 让我们从常规函数开始。 该函数称为stuff_dogs_say() 。 它只是将回声出“狗说猫吸”它被调用后。 我们已经在用户定义的函数文章中介绍了如何调用常规函数。

Now, let’s convert the regular function into an anonymous function.

现在,让我们将常规函数转换为匿名函数。

We create a variable called $stuff_dogs_say and assign the anonymous function to it. Since we are assigning something to the variable, we need to end the assignment statement with a semi-colon like we always do. The function is spaced out onto multiple lines for clarity. Since $stuff_dogs_say contains the anonymous function, we can call that function by appending a couple of parentheses to the variable name itself: $stuff_dogs_say().

我们创建一个名为$ stuff_dogs_say的变量,并将匿名函数分配给它。 由于我们正在为变量赋值,因此需要像往常一样以分号结尾赋值语句。 为了清楚起见,该功能以多行隔开。 由于$ stuff_dogs_say包含匿名函数,我们可以通过在变量名称本身后面加上几个括号来调用该函数: $ stuff_dogs_say()

We can declare parameters and pass arguments to the anonymous function just like we do with regular functions.

就像常规函数一样,我们可以声明参数并将参数传递给匿名函数。

We declare the anonymous function with the parameter $saying and assign it to the $stuff_dogs_say_two variable. The anonymous function just echoes out the argument that’s passed to it. During the function call on line 7, the argument Cats Stink is passed to the $stuff_dogs_say_two function.

我们使用参数$ saying声明匿名函数,并将其分配给$ stuff_dogs_say_two变量。 匿名函数只是相呼应的是,真实传递给它的参数。 在第7行的函数调用期间,参数Cats Stink被传递到$ stuff_dogs_say_two函数。

Let’s look at something interesting. We know that we can return any data type from inside of a function. That means that we can return functions as well; I know, mind-blown. We’re going to create a regular function that returns an anonymous function. Let’s jump into an example and walk through it.

让我们看一些有趣的东西。 我们知道我们可以从函数内部返回任何数据类型。 这意味着我们也可以返回函数。 我知道,令人震惊。 我们将创建一个返回匿名函数的常规函数​​。 让我们跳入一个示例并逐步完成它。

  1. We start by creating a regular function called stuff_cats_say(). The regular function just returns an anonymous function. The anonymous function contains an echo statement: nothing revolutionary here. We know that we can return anything here so we could have returned an integer (i.e. return 3;) or a string (i.e. return “Dino”;). We’re just returning an entire function this time (i.e. return function() { echo “Dogs Suck”; };)

    我们首先创建一个名为stuff_cats_say()的常规函数​​。 常规函数只是返回一个匿名函数。 匿名函数包含一个echo语句:这里没有革命性的内容。 我们知道我们可以在此处返回任何内容,因此我们可以返回整数(即return 3;)或字符串(即return“ Dino”; )。 这次我们只是返回一个完整的函数(即return function(){echo“ Dogs Suck”;}; )

  2. We assign the regular function stuff_cats_say() to the $cats variable on line 9. Remember how PHP works. It starts on the right-hand-side of the assignment operator. It evaluates the stuff_cats_say() function. That function just returns an anonymous function. That anonymous function is then assigned to the $cats variable. It would be exactly the same as saying: $cats = function() { echo “Dogs Suck”; };

    我们在第9 行将常规函数stuff_cats_say()分配给$ cats变量。记住PHP的工作方式。 它从赋值运算符的右侧开始。 它评估stuff_cats_say()函数。 该函数只是返回一个匿名函数。 然后将该匿名函数分配给$ cats变量。 这和说的完全一样: $ cats = function(){echo“狗吸”; };

  3. Now that we know that the $cats variable contains the anonymous function, we know how to call that anonymous function: by appending a couple of parentheses to the variable.

    现在我们知道$ cats变量包含匿名函数,我们知道如何调用该匿名函数:通过在变量后面加上几个括号。

  4. The $cats() function call will call the anonymous function. What does the anonymous function do? It just echoes out Dogs Suck.

    $ cats()函数调用将调用匿名函数。 匿名函数有什么作用? 它只是呼应 狗吸。

Why use anonymous functions over regular functions? Sometimes you need to access a variable that’s outside of the scope of the function and for whatever reason the variable can’t be passed as an argument. You can use the “use” keyword with an anonymous function to gain access to the scope of that variable. We’ll look at the “use” keyword in the next article.

为什么在常规函数上使用匿名函数? 有时,您需要访问超出函数范围的变量,并且无论出于何种原因,都不能将该变量作为参数传递。 您可以将“ use”关键字与匿名函数一起使用,以访问该变量的范围。 我们将在下一篇文章中查看“ use”关键字。

<?php
// Anonymous Functions


function function_name($parameter_name) {
    // body
}


$stuff_dogs_say = function() {
    echo "Dog says Cats Suck<br>";
};


$stuff_dogs_say();


$stuff_dogs_say_two = function( $saying ) {
    echo $saying;
};


$stuff_dogs_say_two( "Cats Stink<br>" );


function stuff_cats_say() {
    return function() {
        echo "Dogs Suck";
    };
}


$cats = stuff_cats_say();
$cats();


?>

翻译自: https://medium.com/dev-genius/php-7-x-p39-anonymous-functions-75f6f99fa571

.net 匿名函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值