On 4 October 2015 at 10:16, Ben Scholzen 'DASPRiD' <m...@dasprids.de> wrote:
>
> you could just do this:
>
> return function ($data) { return $this->genericValidation($data); }
>

> you could just do this:
>
> return function ($data) { return $this->genericValidation($data); }

Yes, there are workarounds possible that don't need anything new i.e.
either implementing the closure generation function in userland or
doing a programmer generated closure.

The downside to writing the closure by hand is more obvious when the
parameters and return value is typed:

class Validator
{
    private function genericValidation(callable $foo, string bar,
UserData $userData) : string {...}

    public function getValidatorCallback($validationType) {
        //...
        return function (callable $foo, string bar, UserData
$userData) : string {
            return $this->genericValidation($data);
        };
    }
}

All of the type checking has to be repeated in the programmer
generated closure, which is annoying as it means you have to keep code
in different places synchronized.

Additionally PHP is kindof slow in function calling. Wrapping the
method to be called in a userland land function makes calling it be
much slower than using an internally generated closure. In addition to
the function call overhead, all of the paramters and return type will
have their type checked twice; once when they're pased to the closure
and once when they're passed to the method.

cheers
Dan

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to