About php7 Uniform Variable Syntax, nested functions

117
September 08, 2018, at 11:30 PM

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?

Answer 1

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
Answer 2

This is more about nested recursive functions, or currying, than that rfc. That rfc just enabled the syntax that supported it.

This uses recursion until you pass a callable:

function test($var) {
    $values = [$var];
    $function = function($callback) use (&$values, &$function) {
        if (is_callable($callback)) {
            return array_reduce(array_slice($values, 1), $callback, $values[0]);
        }
        $values[] = $callback;
        return $function;
    };
    return $function;
}

Because your functions expect two parameters but your nesting could have unlimited parameters, it's best to use an array and array reduce.

However, since multiplication functions like pow won't work with a null initial value, you can specify the initial value as the first passed parameter from the array.

READ ALSO
Is PHP&#39;s password_hash FIPS compliant?

Is PHP's password_hash FIPS compliant?

I believe hash('sha256', $pw) is FIPS compliant, but I know for certain that an attack vector is possible with using that functionAlso, there is no salt (so I would have to encounter that implementation and I would rather not)

64
Find the number of orders related to each order size in MySQL

Find the number of orders related to each order size in MySQL

Need some help with a MySQL query to be used in a larger databaseSimplified here, I need to find the number of orders related to each order size

118
Nested SELECT, or am I over-complicating?

Nested SELECT, or am I over-complicating?

I have the following tables:

67
Batch process a Queryset to avoid database connection timeout

Batch process a Queryset to avoid database connection timeout

I've got a process which imports data from a CSV file then processes the data to run various calculations & set values

108