【发布时间】:2015-08-02 03:41:07
【问题描述】:
是否可以在 PHP 中使用函数来包含文件?我已经尝试了一些东西,但该文件未包含在脚本中。
我正在使用这个功能:
function run($entry_point){
if(!empty($entry_point)){
if(file_exists('assets/controllers/'.$entry_point.'.php'))
include 'assets/controllers/'.$entry_point.'.php';
else
include 'assets/controllers/home.php';
}
}
我的问题是,使用这种函数,索引不会包含$entry_point 文件。
Index.php代码:
session_start();
run($_GET['location']);
谢谢!
【问题讨论】:
-
is it possible to include a file using a function in PHP?是的 -
"如果包含出现在调用文件中的函数内部,则被调用文件中包含的所有代码的行为就像在该函数中定义一样。 " source
-
好的,有更简洁的方法吗?
-
最好的方法是使用自动加载器.... google PSR 0 或 PSR 4(取决于您是否使用命名空间)或 spl_autoload