/ Published in: PHP
Prior to 5.3, anonymous functions (lambdas) can be achieved via the create_function PHP function. This allows for the anonymous functions to be passed as parameters to another function as well, illustrated in this example snippet.
Expand |
Embed | Plain Text
<?php // Pass anonymous function (lambda) as parameter prior to PHP 5.3 function processAnonymousFunction($a = NULL) { // If parameter passed is a string and a function by that name exists (as // should exist with create_function), call the function $a(); } else { echo 'No valid anonymous function passed!'; } } // Outputs: 'Anonymous function (lambda) passed successfully!' processAnonymousFunction(create_function('','echo "Anonymous function (lambda) passed successfully!";')); echo '<br>'; // Outputs: 'No valid anonymous function passed!' processAnonymousFunction(); ?>
You need to login to post a comment.
