别名(使用...作为...)目录中的所有文件
Aliasing (use ... as ...) all files in a directory
我目前正在尝试优化我们开发网站所用的框架,而困扰我的一件事是我们 类 的别名。现在我们有一个巨大的列表,其中包含所有需要的 类,我们必须根据需要 add/remove 类。
我希望它尽可能自动化。我们在每个网站上使用了四个 类 的文件夹,所以我尝试了以下操作:
$directories = array(
'../classes/site/database',
'../classes/site/utils',
'../classes/creabea/utils',
'../classes/creabea/database'
);
foreach($directories as $dir){
$dir_contents = new DirectoryIterator($dir);
foreach($dir_contents as $item){
if(!$item->isDot()){
if($item->isDir()){
foreach(new DirectoryIterator($item->getPath().'/'.$item->__toString()) as $file_l2){
if(!$file_l2->isDot()){
$temppath = preg_replace('(\.\./)', '', $file_l2->getPath());
$path = preg_replace('/\//g', '\', $temppath);
$classname = preg_replace('\.class\.php', '', $file_l2->__toString());
use $path.'\'.$classname;
}
}
} else {
$temppath = preg_replace('(\.\./)', '', $item->getPath());
$path = preg_replace('/\//g', '\', $temppath);
$classname = preg_replace('\.class\.php', '', $item->__toString());
use $path.'\'.$classname;
}
}
}
}
最后它没有起作用,因为你不能在函数或循环或类似的东西内部调用 use
:use
总是必须在全局范围内。
是否有另一种方法可以使别名成为一个自动化过程,同时仍然保持全局范围?
来自http://php.net/manual/en/language.namespaces.importing.php#language.namespaces.importing.scope
importing is done at compile time and not runtime, so it cannot be block scoped.
您不能将 use 语句包含在 if 块中,也不能使用任何运行时的东西来自动导入。
一种方法是使用自动扫描并在该命名空间内添加 类 的预处理器。
我目前正在尝试优化我们开发网站所用的框架,而困扰我的一件事是我们 类 的别名。现在我们有一个巨大的列表,其中包含所有需要的 类,我们必须根据需要 add/remove 类。
我希望它尽可能自动化。我们在每个网站上使用了四个 类 的文件夹,所以我尝试了以下操作:
$directories = array(
'../classes/site/database',
'../classes/site/utils',
'../classes/creabea/utils',
'../classes/creabea/database'
);
foreach($directories as $dir){
$dir_contents = new DirectoryIterator($dir);
foreach($dir_contents as $item){
if(!$item->isDot()){
if($item->isDir()){
foreach(new DirectoryIterator($item->getPath().'/'.$item->__toString()) as $file_l2){
if(!$file_l2->isDot()){
$temppath = preg_replace('(\.\./)', '', $file_l2->getPath());
$path = preg_replace('/\//g', '\', $temppath);
$classname = preg_replace('\.class\.php', '', $file_l2->__toString());
use $path.'\'.$classname;
}
}
} else {
$temppath = preg_replace('(\.\./)', '', $item->getPath());
$path = preg_replace('/\//g', '\', $temppath);
$classname = preg_replace('\.class\.php', '', $item->__toString());
use $path.'\'.$classname;
}
}
}
}
最后它没有起作用,因为你不能在函数或循环或类似的东西内部调用 use
:use
总是必须在全局范围内。
是否有另一种方法可以使别名成为一个自动化过程,同时仍然保持全局范围?
来自http://php.net/manual/en/language.namespaces.importing.php#language.namespaces.importing.scope
importing is done at compile time and not runtime, so it cannot be block scoped.
您不能将 use 语句包含在 if 块中,也不能使用任何运行时的东西来自动导入。
一种方法是使用自动扫描并在该命名空间内添加 类 的预处理器。