<?php
class A
{
public $closure;
public static function myFunc($input): string
{
$output = $input . ' is Number';
return $output;
}
public static function closure(): Closure
{
return function ($input) {
return self::myFunc($input);
};
}
public static function run()
{
$closure = self::closure();
echo $closure(1); // 1 is Number
self::$closure = $closure;
echo self::$closure(2); // Fatal error
}
}
A::run();
Run Code Online (Sandbox Code Playgroud)
我想绑定self::closure()到self::$closure,并在内部使用它,但是它消失在某个地方。如何在PHP中将闭包绑定到静态类变量?