在 file1.php 我有
include 'database_functions.php';
// bunch of other stuff
function render_php($path){
ob_start();
include($path);
$var=ob_get_contents();
ob_end_clean();
return $var;
}
echo render_php('file2.php');
然后在 file2.php 我有:
include 'database_functions.php';
$stmt = $db->prepare(" // do stuff ");
....
我遇到的问题是,如果我将 database_functions.php 添加到 file2 我会收到一个错误,如果我不将 database_functions.php 添加到 file2 我也会收到一个错误。
如果没有 file2 中的 database_functions.php,我得到:
Undefined variable: db
使用 file2 中的 database_functions.php 我得到:
Constant DBHOST already defined
所以显而易见的问题是我如何渲染 file2.php 而不会出错。(包括/要求在这种情况下不起作用)。