我遇到一个非常奇怪的问题,我在通过Undefined variable: authenticated_function
调用后立即抛出isset()
异常。代码:
class AuthService
{
static $authenticated_function;
public static function setAuthenticatedFunction($func)
{
\Log::info("Function Set");
self::$authenticated_function = $func;
}
public function authenticated()
{
\Log::info("ISSET: " . isset(self::$authenticated_function));
var_dump(self::$authenticated_function);
if(isset(self::$authenticated_function))
self::$authenticated_function(); //Exception is thrown here
}
}
在我的日志文件中:
[2016-12-19 19:05:08] local.INFO: Function Set
[2016-12-19 19:05:08] local.INFO: ISSET: 1
var_dump()
:
object(Closure)[103]
public 'this' =>
object(App\Providers\AppServiceProvider)[86]
... //Removed for brevity
protected 'defer' => boolean false
PHP 7.0.8
Laravel 5.3
答案 0 :(得分:2)
你有这个错误,因为PHP尝试执行你的指令:
self:: + [ $authenticated_function + () ]
因此,您的变量不存在...... Undefined variable: authenticated_function
如果要执行该功能,可以使用以下方式:
$func = self::$authenticated_function;
$func();
或更好:
call_user_func(self::$authenticated_function /*, $param1, $param2*/);
// or
call_user_func_array(self::$authenticated_function /*, [$param1, $param2]*/);
:)