Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Lambdas (Anonymous functions)

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 245
    Comment on it

    The concept of lambda expression was introduced after coming the PHP version 5.3. We use generally any expression to make code cleaner and intuitive. and lambda expression helps to perform the same concept.

     

    Lambdas (Anonymous functions) are the function having no name. These functions have same kind of behavior as a general function does. They contain a block of code and for using them we call them also we can pass argument to them and get value by returning value within function.

     

     

    Difference between anonymous function and a general function is :-

    1. Anonymous function have no name between keyword function and the parenthesis.
    2. We insert semicolon just after definition of function. reason behind this is defining of anonymous function are basically expression where code construct is the definition of regular function.

     

    Below mentioned example for anonymous function will explain it better way.

    <?php
    
    //define anonymous function     
    
    function() {
          return 'Hello, World!';
    };
    
    ?>

     

    We assign this function to a variable and then call it by using variable name and call like below code.
     

    <?php
    
    $test = function() {
    
        return 'Hello, World!';
    };
    
    //calling
    
    echo $test();
    
    ?>

     

    NOTE :-  If we pass it to other function and call it later then this scenario is known as callbacks. see the example below.

     

    <?php
    
    // This function uses a callback function.
    
    function doIt($callback)
    {
        $data = "this is my data";
        $callback($data);
    }
    
    
    // This is a sample callback function for doIt().
    function myCallback($data)
    {
        print 'Data is: ' .  $data .  "\n";
    }
    
    
    // Call doIt() and pass our sample callback function's name.
    doIt('myCallback');
    
    ?>

     

    NOTE :- If we return it from within any outer function  so that we can access it globally by function variable then this scenario is known as closure.

     

    <?php 
    
    // A simple example of a closure
     
    function getGreetingFunction() {
     
      $timeOfDay = "morning";
     
      return ( function( $name ) use ( &$timeOfDay ) {
        $timeOfDay = ucfirst( $timeOfDay );
        return ( "Good $timeOfDay, $name!" );
      } );
    };
     
    $greetingFunction = getGreetingFunction();
    echo $greetingFunction( "Fred" ); // Displays "Good Morning, Fred!"
    
    ?>

     

    In the  above code getGreetingFunction() we can see initialization of a local variable named as $timeOfDay, then we are defining and returning an anonymous function.

    This anonymous function manipulate $timeOfday variable by making its first letter on upper case.. Here we used use keyword because anonymous function have no access to $timeOfday and variable is local to getGreetingFunction().

     

    use keyword make its scope to global access and ampersand (&) tells PHP to pass the reference to $timeOfDay within anonymous function so that anonymous function manipulate this variable directly.

     

    Now we can store the manipulate value in $greetingFunction by calling getGreetingFunction(). and call anonymous function that manipulates value in the $timeofDay variable within the closure and returns a greeting containing new value of $timeOfDay, and we ge the output on the current case is 'MORNING'.

     

    Hope this blog helps you to knowing the concept for  lambda(anonymous) functions ,closures and callbacks in PHP.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: