Php 我可以通过传递类路径而不是类名来获取反射数据吗?
我正在制作自己的类自动加载器,并根据结果构建一个数组 我希望它看起来像我的一个阵列Php 我可以通过传递类路径而不是类名来获取反射数据吗?,php,reflection,Php,Reflection,我正在制作自己的类自动加载器,并根据结果构建一个数组 我希望它看起来像我的一个阵列 $classes['myTestClass']=./classes/myTestClass.class.php 我可以构建这个数组,但我不能做的是将要加载的类的实际类名设置为路径的键 这是我构建数组的foreach循环 foreach ($classIterator as $file) { if ($this->_searchFilter($file)) $this->cla
$classes['myTestClass']=./classes/myTestClass.class.php
我可以构建这个数组,但我不能做的是将要加载的类的实际类名设置为路径的键
这是我构建数组的foreach
循环
foreach ($classIterator as $file) {
if ($this->_searchFilter($file))
$this->classes[] = $file->getPathname();
}
$file->getPathname()
具有类文件的绝对路径,该类文件将是必需的
,如何从该文件中获取类名?如果您使用命名约定,那么/classes/myTestClass.class.php
声明myTestClass
,那么简单的stru replace
就足够了:
foreach ($classIterator as $file) {
if ($this->_searchFilter($file))
$classPath = $file->getPathname();
$className = str_replace('.class.php', '', basename($classPath));
$this->classes[$className] = $classPath;
}