Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
$array = array('array_map1','array_map2');
// old "lambda"
echo "---\nold lambda style 'create_function\n";
$createdFunction = create_function('$arg','echo $arg . PHP_EOL;');
$createdFunction('direct');
call_user_func($createdFunction, 'call_user_func');
array_map($createdFunction, $array);
// old lambda looks like eval'd code ... ?
echo "---\nown creation 'my_create_function\n";
function my_create_function ($args, $code) {
static $index = 0;
$index++;
$name = "my_lambda_$index";
$evaled = "function $name ($args) {{$code}}";
eval($evaled);
return $name;
}
$myCreatedFunction = my_create_function('$arg','echo $arg . PHP_EOL;');
$myCreatedFunction('direct');
call_user_func($myCreatedFunction, 'call_user_func');
array_map($myCreatedFunction, $array);
// Callable by variable resolution ($function($a) => outputFunction($a) => call)
echo "---\nown creation 'my_create_function\n";
function outputFunction ($arg) {
echo $arg . PHP_EOL;
}
$function = 'outputFunction';
$function('direct');
call_user_func($function, 'call_user_func');
array_map($function, $array);
// Callable by call_user_func(_array)
echo "---\nStatic class array 'array(classname,methodname)'\n";
class StaticClass {
public static function output ($arg) {
echo $arg . PHP_EOL;
}
}
$staticClass = array('StaticClass', 'output');
#$staticClass('direct');
call_user_func($staticClass, 'call_user_func');
array_map($staticClass, $array);
// Callable by call_user_func(_array)
echo "---\nobject array 'array(\$obj,method)'\n";
class InstanceClass {
public function output ($arg) {
echo $arg . PHP_EOL;
}
}
$instanceClass = array(new InstanceClass, 'output');
#$instanceClass('direct');
call_user_func($instanceClass, 'call_user_func');
array_map($instanceClass, $array);
// New invokeable class
echo "---\nNew invokable class '\$obj->__invoke()'\n";
class InvokeableClass {
public function __invoke ($arg) {
echo $arg . PHP_EOL;
}
}
$invokeableClass = new InvokeableClass;
$invokeableClass('direct');
call_user_func($invokeableClass, 'call_user_func');
array_map($invokeableClass, $array);
// New lambda
echo "---\nLambda function\n";
$lambda = function ($arg) {
echo $arg . PHP_EOL;
};
$lambda('direct');
call_user_func($lambda, 'call_user_func');
array_map($lambda, $array);
// New closure. Like lambda, but "integrated" values
echo "---\nClosure\n";
$fromScope = 'From outside';
$closure = function ($arg) use ($fromScope) {
echo "$arg with '$fromScope'" . PHP_EOL;
};
$closure('direct');
call_user_func($closure, 'call_user_func');
array_map($closure, $array);
<?php
// old lambda looks like eval'd code ... ?
echo "---\nown creation 'my_create_function\n";
function my_create_function ($args, $code) {
static $index = 0;
$index++;
$name = "my_lambda_$index";
$evaled = "function $name ($args) {{$code}}";
eval($evaled);
return $name;
}
$myCreatedFunction = my_create_function('$arg','echo $arg . PHP_EOL;');
$myCreatedFunction('direct');
call_user_func($myCreatedFunction, 'call_user_func');
array_map($myCreatedFunction, $array);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment