If you want to create anonymous in PHP <5.3, you can use create_function . Also, information about callbacks is interesting here (it may be useful).
create_function
# This (function in other variable is only for cleaner code) $func = create_function('', "echo 'This is example from anoymus function';"); $exampleArray = array( 'func' => $func );
But you can do the same liek code above with an alternative way:
# Create some function function func() {
The code above creates a function that does something, then we save the function name in a variable (it can be passed as a parameter, etc.).
The calling function, when we know only its name:
First way
$func();
Alternative
call_user_func($func);
So, an example that connects all of the above:
function primitiveArrayStep(&$array, $function) { # For loop, foreach can also be used here for($i = 0; $i < count($array);$i++) { # Check if $function is callable if( is_callable($function) ) { # Call function $function(&$array[$i]); } else { # If not, do something here } } }
And using the function above:
$array = array('a', 'b', 'c'); $myFunction = create_function('&$e', '$e = $e . " and i was here";'); primitiveArrayStep($array, $myFunction); echo '<pre>'; var_dump($array);
Return:
array(3) { [0]=> string(16) "a and i was here" [1]=> string(16) "b and i was here" [2]=> string(16) "c and i was here" }
References:
Robik source share