我想写一些代码来导入和'使用'(设置别名)到由名称空间分隔的类。
这个过程通常的常规方法是:
require_once('/tools/toola/mytool.php');
use tools'toola'MyTool as MyTool;
有可能有这样的东西吗?
function import($path, $class_name) {
$file_path = strtolower($path . '/' . $class_name . '.php');
if(file_exists($file_path)) {
require_once($file_path);
use $file_path as $class_name; //Line currently throws syntax error
} else { //file not found }
}
因此,我可以调用import('/tools/toola', 'mytool');自动化。
这行似乎有一些语法错误,不太合适。是否有一种方法,我可以使用有实现的功能?
PHP manual:
use关键字必须在文件的最外层作用域中声明全局作用域)或在命名空间声明中。这是因为导入是在编译时完成的,而不是运行时,所以不能块作用域。