Php 错误:路径不能为空,在闭包函数内部

Php 错误:路径不能为空,在闭包函数内部,php,closures,Php,Closures,我正在尝试构建一个模型/视图/控制器框架,我想包括我的视图文件。 我从codeigniter复制了代码,然后编辑了它: if( ! is_file($file_location)){ throw new Exception("`$name` is Not Found in Views"); } $output = (function (): string { // extract($data); ob_start(); include $fil

我正在尝试构建一个
模型/视图/控制器
框架,我想包括我的视图文件。
我从
codeigniter
复制了代码,然后编辑了它:

if( ! is_file($file_location)){
    throw new Exception("`$name` is Not Found in Views");
}
$output = (function (): string {
    // extract($data);
    ob_start();
    include $file_location;
    return ob_get_clean() ?: '';
})();// this is line 25
我得到一个例外,我抓住了它:

Error:Path cannot be empty
line        25  
function        {closure}

也许这应该奏效:

if( ! is_file($file_loc)){
    throw new Exception("`$name` is Not Found in Views");
}
$output = (function () use ($file_loc): string {
    // extract($data);
    ob_start();
    include $file_loc;
    return ob_get_clean() ?: '';
})();// this is line 25

我假设这是由于回调范围中没有定义
$file\u location
。你忘了
使用它了吗?我编辑了我的问题,添加了我检查的内容,我也回显了位置,它是
(C:\xampp\htdocs\pulse\app\Views\user.php)
在回调之外检查不会改变任何事情,因为该变量在匿名函数中仍然不存在。另外,在
if
中,您使用
$file\u loc
,而在回调中它位于
$file\u位置
。这是否回答了您的问题?我们通常希望避免重复内容。网站上已经有关于如何在闭包中使用外部变量的解释,应该将该问题标记为重复。