Php 如何保存call\u user\u func\u array()函数输出

Php 如何保存call\u user\u func\u array()函数输出,php,function,Php,Function,我试图使一个静态站点生成器与一个伟大的路由类一起工作 if(php\u sapi\u name()=“cli”){ foreach($router->get()作为$route){ $out=ob_get_contents(); 调用用户函数数组($route['function'],array()); ob_end_clean(); 文件内容(“./temp/”$route['expression'..”.html“,$out); } } 所以我试着这样做,我可以通过终端看到编译的html代

我试图使一个静态站点生成器与一个伟大的路由类一起工作

if(php\u sapi\u name()=“cli”){
foreach($router->get()作为$route){
$out=ob_get_contents();
调用用户函数数组($route['function'],array());
ob_end_clean();
文件内容(“./temp/”$route['expression'..”.html“,$out);
}
}
所以我试着这样做,我可以通过终端看到编译的html代码,但文件是空的。 如何保存call\u user\u func\u array()函数的输出?

请参见此示例:

if (php_sapi_name() === "cli") {
    // Start output buffering here
    ob_start();
    foreach ($router->get() as $route) {
        call_user_func_array($route['function'], array());
        // get output of `$route['function']` to $out variable
        $out = ob_get_contents();
        // clean buffer
        ob_clean();

        file_put_contents("./temp/" . $route['expression'] . ".html", $out);
    }
    // stop output buffering
    ob_end_clean();
}

您需要在调用函数之后而不是之前调用
ob\u get\u contents()
。您还可以将
ob\u get\u contents()
ob\u end\u clean()
ob\u get\u clean()
组合起来,我现在就试试。谢谢你的评论,我仍然可以在文件中看到终端的输出。仍然是一样的。你不需要在迭代之间清理输出缓冲区吗?否则,每个文件都将包含以前文件的内容。@Barmar您是对的,将在一分钟内修复。确定。我会努力的。太好了!它工作得很好。非常感谢。