phparrow-functionsphp-7.4

How to use arrow functions in PHP?


I got to know about arrow functions in PHP 7.4. I tried using them like

<?php
$num = 1;
$arrowfunction = () => {
   return $num + 1;
}
echo $arrowfunction();

Because I saw the => operator in the pull request. Just like javascript.

I expected '2' as the output but this didn't work! I got

Parse error: syntax error, unexpected ')' in /test.php on line 3


Solution

  • Arrow functions in PHP are introduced in PHP 7.4. They are a little different.

    The fn keyword

    The new fn keyword is now a reserved keyword.

    Previously, we used to continue using function keyword.

    $add = function ($valone,$valtwo) {
        return $valone + $valtwo;
    };
    $add(1,2) // 3
    

    With the advent of new arrow functions:

    $add = fn($valone,$valtwo) => $valone + $valtwo;
    $add(1,2) // 3
    

    Parent scope

    Earlier, we have to follow with the usage of the keyword use for the involvement of a variable from the parent scope.

    $y = 1;
    $fn = function ($x) use ($y) {
        return $x + $y;
    };
    echo $fn(2); // 3
    

    The expression defined in the parent scope will be implicitly captured by-value.

    $y = 1;
    $fn = fn($x) => $x + $y;
    echo $fn(2); // 3
    

    The above follows for $this variable inside class methods.

    class foo {
       public function test() {
           $context = fn() => var_dump($this);
           $context(); 
       }
    }
    $test = new foo();
    $test->test();  // object(foo)#1 (0) { }
    

    Just like previously, we used to perform our operations by using the use keyword to take a variable from the parent scope, so this means that we cannot write the value of the variable from the function into the upper scope.

    $y = 1;
    $fn = fn() => $y++;
    $fn(); // Has no effect
    echo $y  // 1
    

    If we are thinking of assigning another variable's value from the closure then this also will not work

    $y = 1;
    $f = 0;
    $fn = fn() => $f = $y + 1;
    $fn();
    echo $f; // 0
    

    Function signatures

    This is completely new in PHP, this allows us the define the type of function, variable and the value the function is returning

    fn(int $x) => $x; // the argument type must be (int)
    fn(): int => $x; // type of return value (int)
    

    Errors are thrown when the defined argument type is not placed in the argument when calling the function. The error can be caught by using the TypeError type

    $var = 10;
    $int_fn = fn(int $x): int => $x;
    var_dump($int_fn($var)); // int(10)
    try {
        $int_fn("foo");
    } catch (TypeError $e) {
        echo $e->getMessage(), "\n"; // Argument 1 passed to {closure}() must be of the type int, string given, called in x on line y
    }
    

    By PHP 7.1, they support the ?type in arguments which allows the argument to be null too.

    $funn = fn(?int... $args): array => $args;
    var_dump($funn(20, null, 30)); // Array(3) { [0]=> int(20) [1]=> NULL [2]=> int(30) }
    

    If you supply a string or anything else rather than int to the above function, then you'll get an error

    Argument passed to {closure}() must be of the type int or null, string given, called in x on line y

    Nested arrow functions

    $var = 6;
    var_dump((fn() => fn() => $var)()());  // int(6)
    var_dump((fn() => function() use($var) { return $var; })()()); // int(6)
    

    Any possible errors inside the closure are not thrown unless called

    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    $b = 1;
    fn() => $b + $c; // no error, nothing
    
    
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    $b = 1;
    (fn() => $b + $c)(); // Notice: Undefined variable: c in the location on line x
    

    If error reporting is off then you'll just get int(1)

    How to use PHP. 7.4 now?
    For quick online testing just paste these code there

    For your native system, I Just cloned this branch of php-src and compiled it using GCC and make. I did my testing via a test.php file and command line to check if everything works.

    Core reference - https://wiki.php.net/rfc/arrow_functions_v2