Viewed   95 times

Is it possible to have a PHP function that is both recursive and anonymous? This is my attempt to get it to work, but it doesn't pass in the function name.

$factorial = function( $n ) use ( $factorial ) {
    if( $n <= 1 ) return 1;
    return $factorial( $n - 1 ) * $n;
};
print $factorial( 5 );

I'm also aware that this is a bad way to implement factorial, it's just an example.

 Answers

1

In order for it to work, you need to pass $factorial as a reference

$factorial = function( $n ) use ( &$factorial ) {
    if( $n == 1 ) return 1;
    return $factorial( $n - 1 ) * $n;
};
print $factorial( 5 );
Monday, September 5, 2022
2

You can't. You'd have to call it first. And since PHP doesn't support closure de-referencing yet, you'd have to store it in a variable first:

$f = function(){
    $data = array(
        'fruit'     => 'apple',
        'vegetable' => 'broccoli',
        'other'     => 'canned soup');
    return $data;
};
myfunction($f());
Monday, November 28, 2022
 
alles
 
3

First off, there doesn't seem to be a consensus definition for inline functions in JavaScript. I consider an inline function to be a special case of a JavaScript function. An inline function is a function assigned to a variable that is created at runtime instead of at parsetime.

Anonymous functions and inline functions are practically the same, in that they are created at runtime. The difference is that an inline function is assigned to a variable and so it can be reused. In that way, inline functions work the same as a regular function.

Pre es6, anonymous and inline functions were declared similarly to regular functions using the function keyword. With the advent of es6, anonymous and inline functions could also be declared with the more compact, arrow function syntax.

Function

function func() {
    alert ('function');
} 
$('a').click(func);

Inline Function

var func = function() { 
    alert ('inline') 
};
$('a').click(func);

// Alternative es6+ inline arrow function.
let func2 = () => alert('inline');
$('a').click(func2);

Anonymous Function

$('a').click(function() {
    alert('anonymous');
});
// Alternative es6+ anonymous arrow function.
$('a').click(() => alert('anonymous'));
Wednesday, November 30, 2022
 
4

The i in the anonymous function captures the variable i, not its value. By the end of the loop, i is equal to somearray.length, so when you invoke the function it tries to access an non-existing element array.

You can fix this by making a function-constructing function that captures the variable's value:

function makeFunc(j) { return function() { console.log(somearray[j][0]); } }

for (var i = 0; i < somearray.length; i++)
{
    myclass.foo({'arg1':somearray[i][0]}, makeFunc(i));
}

makeFunc's argument could have been named i, but I called it j to show that it's a different variable than the one used in the loop.

Sunday, December 18, 2022
 
5

Anonymous functions replaced inline functions (as mentioned in both the docs and in the link you posted)

The docs warn:

inline will be removed in a future release. Use Anonymous Functions instead.

Saturday, September 24, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :