Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About php7 Uniform Variable Syntax, nested functions

I'm try to solve a task which uses new functions php7 uniform variable syntax nested () support foo()() (https://wiki.php.net/rfc/uniform_variable_syntax). I need write function test for this code:

$sum = function($a, $b)  { return $a + $b; };
test(6)(2)(3)($sum);    // 11
test(3)(1)($sum);       // 4
test(3)(3)('pow');      // 27

I don't found any explanation for this feature. Where can I find how to use it? I see that I must return function name in function test, but how to pass argument?

like image 470
shady2k Avatar asked May 16 '26 05:05

shady2k


1 Answers

Thanks all for help. It's something like this:

<?php

function test($a) {
    echo '<br/>';
    $arr[] = $a;
    return $mf = function($b) use(&$mf, &$a, &$arr) {
        if(gettype($b) == 'object') {
            echo(array_reduce($arr, $b));
        } elseif (gettype($b) == 'string') {
            if($b == 'pow') {
                echo array_reduce($arr, function ($carry, $a) {
                    return !empty($carry) ? pow($carry, $a) : $a;
                });
            }
        } elseif (gettype($b) == 'integer') {
            $arr[] = $b;
        }
        return $mf;
    };
}

$sum = function($a, $b)  { return $a + $b; };
test(6)(2)(3)($sum);    // 11
test(3)(1)($sum);       // 4
test(3)(3)('pow');      // 27
like image 157
shady2k Avatar answered May 18 '26 21:05

shady2k



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!