首页 > 解决方案 > 错误:路径不能为空,在闭包函数内部

问题描述

我正在尝试构建一个Model/View/Controller框架,我想包含我的视图文件。
我从复制代码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}

标签: phpclosures

解决方案


也许这应该工作:

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

推荐阅读