class Foo { public function bar() { return "Hello, world!"; } } $reflectionMethod = new ReflectionMethod('Foo', 'bar'); $closure = $reflectionMethod->getClosure(new Foo()); echo $closure();
$sum = function($a, $b) { return $a + $b; }; $reflectionMethod = new ReflectionMethod($sum, '__invoke'); $closure = $reflectionMethod->getClosure($sum); echo $closure(2, 3);This code defines an anonymous function that takes two parameters and returns their sum. The `ReflectionMethod` object is used to get the closure of the `__invoke()` method of the anonymous function, which is then called to print the sum of 2 and 3. Package/library: PHP Core.