Is it possible to curry method calls in PHP?

前端 未结 8 2443
日久生厌
日久生厌 2020-12-06 01:17

I have a SoapClient instance generated for a WSDL file. All except one of the method invocations require the username and the password to be passed id.

Is there any

8条回答
  •  旧巷少年郎
    2020-12-06 01:46

    As mentionned by Ihor, the Non-standard PHP library is interesting. I have already implemented the same method, it is a bit different than Ihor's curried() function

    function curryfy($f, $args = []) {
        $reflexion = new ReflectionFunction($f);
        $nbParams = $reflexion->getNumberOfParameters();
    
        return function (...$arguments) use ($f, $reflexion, $nbParams, $args) {
            if (count($args) + count($arguments) >= $nbParams) {
                return $reflexion->invokeArgs(array_merge($args, $arguments));
            }
    
            return curryfy($f, array_merge($args, $arguments));
        };
    }
    

    Usage :

    function display4 ($a, $b, $c, $d) {
        echo "$a, $b, $c, $d\n";
    };
    $curry4 = curryfy('display4');
    display4(1, 2, 3, 4);
    $curry4(1)(2)(3)(4);
    

提交回复
热议问题