在单个语句中创建和调用匿名函数 [英] Creating and invoking an anonymous function in a single statement

查看:118
本文介绍了在单个语句中创建和调用匿名函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用php闭包或匿名函数创建函数而不指定其名称。

A php closure or anonymous function is used to create function without specifying its name.

是否可以调用它们而不像JavaScript中那样分配标识符?
eg

Is it possible to call them without assigning to identifier as we do in JavaScript ? e.g.

(function(){
    echo('anonymous function');
})();

使用同时定义匿名函数,以及在具有私有属性可访问性的公共方法中匿名函数的状态是什么?

What is the correct use of use construct while defining anonymous function and what is the status of anonymous function in public method with accessibility to private properties?

$anon_func = 
function($my_param) use($this->object_property){ //use of $this is erroneous here
    echo('anonymous function');
};


推荐答案


调用他们没有
分配标识符,我们在
JavaScript?例如

Is it possible to call them without assigning to identifier as we do in JavaScript ? e.g.

否,除非在您的方法将回调作为参数时计数。例如:

No, unless you count it when your method takes a callback as an argument. eg:

$square = array_map(function ($v) { return $v*$v; }, $array);




正确使用
construct anonymous
function

What is the correct use of use construct while defining anonymous function

使用当前词汇范围应该导入闭包。您甚至可以通过引用传递它们,并更改要传递的变量,例如:

The use keyword indicates which variables from the current lexical scope should be imported into the closure. You can even pass them by reference and change the variable being passed, eg:

$total = 0;
array_walk($array, function ($v) use (&$total) { $total += $v; });
// $total is now the sum of elements in $array








在public方法中匿名
函数的状态是什么,
对私有属性的可访问性?

what is the status of anonymous function in public method with accessibility to private properties?

在类中定义的闭包可以完全访问其所有属性和方法,包括不需要导入 $ this 的私有 use rel =nofollow noreferrer > PHP 5.4

Closures defined inside a class have full access to all its properties and methods, including private ones with no need to import $this through the keyword use in PHP 5.4:

// this works fine in PHP 5.4
$anon_func = 
function($my_param) { 
    $thing = $my_param + $this->object_property;
    echo('anonymous function');
};

请注意,由于一些奇怪的原因支持 $ this in closure 已在PHP 5.3 中移除。在这个版本中,你可以使用类似的方法解决这个限制:

Note that for some strange reason support for $this in closures was removed in PHP 5.3. In this version, you can work around this restriction using something like:

// a workaround for PHP 5.3
$temp = $this;

$anon_func = 
function($my_param) use ($temp) { 
    $thing = $my_param + $temp->object_property;
    echo('anonymous function');
};

但这只允许你访问公共成员,试图访问私有成员仍会给你一个错误。

But this gives you access to public members only, attempting to access private members will still give you an error.

另请注意,尝试导入 $ this (通过使用),不管PHP版本,将导致致命错误不能使用$ this作为词法变量

Also note that attempting to import $this (via use), regardless of the PHP version, will result in a fatal error Cannot use $this as lexical variable.

这篇关于在单个语句中创建和调用匿名函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆