lundi 10 avril 2017

Why does __invoke not show in ReflectionClass::getMethod result for a closure, but ReflectionClass::hasMethod("__invoke") returns true?

I'm experimenting with reflection and trying to understand closures. Here's my code (PHP version is 5.6):

function closureWithState ($name) {
    return function () use ($name) { // 'use' keyword attaches the closure state
        return strToUpper($name);
    };
}

$closure = closureWithState('Foo');

$reflector = new ReflectionClass($closure);
$methods = $reflector->getMethods();
foreach ($methods as $method) {
    echo $method->getName() . PHP_EOL;
}

echo "Result of hasMethod for __invoke: " .$reflector->hasMethod("__invoke") . PHP_EOL;

and my output is

__construct
bind
bindTo
Result of hasMethod for __invoke: 1

So it seems that getMethods returns the methods __construct, bind,and bindTo but not __invoke. But when I test for hasMethod("__invoke") it returns true.

What is happening here?





Aucun commentaire:

Enregistrer un commentaire