【发布时间】:2020-03-10 15:14:14
【问题描述】:
<?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();
我想将self::closure() 绑定到self::$closure,并在内部使用它,但它在某处消失了。如何在 PHP 中将闭包绑定到静态类变量?
【问题讨论】: