Creating and invoking an anonymous function in a s

2020-02-04 07:17发布

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

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');
};

标签: php closures
4条回答
不美不萌又怎样
2楼-- · 2020-02-04 07:55
call_user_func(function() use(closure-vars){ ... });
查看更多
你好瞎i
3楼-- · 2020-02-04 07:56

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

Not in PHP 5.x; unless you count it when your method takes a callback as an argument. eg:

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

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

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

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');
};

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.

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.

查看更多
Lonely孤独者°
4楼-- · 2020-02-04 08:12

PHP 7 added the ability to do this.

This code:

(function() { echo "This works as expected in PHP 7.\n"; })();

works as one would expect in PHP 7. (It still doesn't work in any PHP 5.x. release)

查看更多
神经病院院长
5楼-- · 2020-02-04 08:14

Doesn't look like it, as they still have to be declared with the function() {} notation, and on my 5.3.2 install, trying your sample notion returns an unexpected '(' syntax error. The doc page on closures doesn't mention it either.

Maybe it'll become possible once they patch up the parser to allow somefunction()[2] array dereferencing.

查看更多
登录 后发表回答