Creating and invoking an anonymous function in a single statement
Asked Answered
D

5

20

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');
};
Damnedest answered 31/8, 2010 at 2:15 Comment(4)
A closure is not entirely synonymous with an anonymous function, although anonymous functions in PHP are of type Closure. Just so you know.Beberg
What is the exact error when you try your second code example? 'erroneous' is a little unspecific.Beberg
Cannot use $this as lexical variableDamnedest
OP: I've updated my answer due to changes in PHP 5.4; don't ask me why they did it. cc @BebergIllusionism
I
11

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.

Illusionism answered 31/8, 2010 at 2:45 Comment(2)
+1 It is not possible to self-invoke anonymous functions like (function() {})(); in PHP though, probably for the same reasons array dereferencing isn't yet doable either as Marc B notes.Beberg
@Beberg BTW array dereferrencing (eg: function foo() { return $someArray; }, $var = foo()['bar']) works in PHP 5.4 as wellIllusionism
B
27

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)

Begin answered 27/1, 2016 at 17:51 Comment(0)
S
20
call_user_func(function() use(closure-vars){ ... });
Salisbarry answered 17/11, 2011 at 13:2 Comment(2)
How would the OP make class properties accessible to the closure?Constellation
The op could create a reference of the object to pass to the closure through the use statement, ie: $obj = $this; call_user_func(function() use ($obj){}). That way he's have access to class properties and methods inside the closure. Bit of a hack but it's the best we have until PHP 5.4 arrives.Nominee
I
11

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.

Illusionism answered 31/8, 2010 at 2:45 Comment(2)
+1 It is not possible to self-invoke anonymous functions like (function() {})(); in PHP though, probably for the same reasons array dereferencing isn't yet doable either as Marc B notes.Beberg
@Beberg BTW array dereferrencing (eg: function foo() { return $someArray; }, $var = foo()['bar']) works in PHP 5.4 as wellIllusionism
W
3

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.

Wiretap answered 31/8, 2010 at 2:25 Comment(1)
Array dereferencing has been added to PHP 5.4, but you still can't call anonymous functions right after their declaration.Illusionism
N
-1

example 1

(new class {
    function __invoke($n) {
        echo "in PHP 7.\n";
    }
})(0);

example 2

(function() { echo "in PHP 7.\n"; })();

example 3

call_user_func(function() { echo "in php> 5.6\n"; });
Neigh answered 29/12, 2021 at 15:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.