Anonymous functions

what are anonymous functions?

Anonymous functions(aka. lambda) originating from the Lambda calculus are similar to our regular functions, in that they contain a block of code that is run when they are called (well, pretty similar and now you all know that before). They can also accept arguments, and return values.

The key difference — as their name implies — is that anonymous functions have no name.
Another difference is that they end in semicolons i.e. function definitions are expressions, whereas regular function definitions are code constructs.

so why do we need functions with no name?

the best solution or answer that can come to my mind is when you think of callbacks.

well, you can refer here and i have pasted the same below for continued reading

since an anonymous function is an expression — much like a number or a string — you can do various handy things with it. For example, you can:

1. Assign it to a variable, then call it later using the variable’s name. You can even store a bunch of different anonymous functions in a single array. – variable functions

2. Pass it to another function that can then call it later. This is known as a callback.

3. Return it from within an outer function so that it can access the outer function’s variables. This is known as a closure.

 

So, to code straight away and here is the example for the first set i.e. variable functions.


<?php
<pre>#use type one for anonymous function
#i.e. variable functions
class VarFuncs{
 public $my_var;
 public function __construct(){
 $this->my_var=function( $name, $timeOfDay ) {
 return ( "Welcome to another glorious $timeOfDay, $name!" );
 };
 }
 #workaround for PHP ver < PHPv7
 function __call($method, $args)
 {
 if (isset($this->$method) && $this->$method instanceof \Closure) {
 return call_user_func_array($this->$method, $args);
 }

 trigger_error("Call to undefined method " . get_called_class() . '::' . $method, E_USER_ERROR);
 } 
}

$object=new VarFuncs();
$check_output=$object->my_var("nightslacker","night");
echo $check_output."\n";
?>

As of PHPv7 we can replace the line 19 with

</pre>
<pre>$check_output=($object->my_var)("nightslacker","night");//note the brackets around the object</pre>
<pre>

 

Now, we can go on and implement the second point i.e. callbacks.

<?php</pre>
<pre>#use type two for anonymous function
#i.e. callbacks</pre>
<pre>
class MainFunc{
private function toUpperCase($receiveArr) {
return strtoupper($receiveArr);
}
public function changeCase(array $arr){
return array_map(array($this,'toUpperCase'),$arr);
}
}
$objMainFun=new MainFunc();

$resultArr=$objMainFun->changeCase(array('nightslacker','changed','to','uppercased nightslacker'));
print_r($resultArr);
?>

you get the result of the array with all caps for the array that’s passed using the callback toUpperCase

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s