Php 限制我的spl_autoloader仅加载命名空间中的类?
我刚刚开始在我的应用程序中使用autoloader惰性加载,我正在与名称空间冲突。自动加载程序正在尝试加载类似于Php 限制我的spl_autoloader仅加载命名空间中的类?,php,namespaces,autoloader,Php,Namespaces,Autoloader,我刚刚开始在我的应用程序中使用autoloader惰性加载,我正在与名称空间冲突。自动加载程序正在尝试加载类似于new DateTime()的内容,但失败了。让我的自动加载器只针对我自己的命名空间类有什么诀窍吗 这是我目前拥有的代码。我怀疑这是一个混乱,但我不知道如何纠正它: <?php namespace RSCRM; class Autoloader { static public function loader($className) { $filename
new DateTime()
的内容,但失败了。让我的自动加载器只针对我自己的命名空间类有什么诀窍吗
这是我目前拥有的代码。我怀疑这是一个混乱,但我不知道如何纠正它:
<?php namespace RSCRM;
class Autoloader {
static public function loader($className) {
$filename = dirname(__FILE__) .'/'. str_replace("\\", '/', $className) . ".php";
if (file_exists($filename)) {
include_once($filename);
if (class_exists($className)) {
return TRUE;
}
}
return FALSE;
}
}
spl_autoload_register('\RSCRM\Autoloader::loader');
我使用的实际上是改编自autoloader,autoloader用于对几个PHP库进行单元测试:
<?php
spl_autoload_register(function ($class) {
// a partial filename
$part = str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php';
// directories where we can find classes
$dirs = array(
__DIR__ . DIRECTORY_SEPARATOR . 'src',
__DIR__ . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'src',
__DIR__ . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR . 'src',
);
// go through the directories to find classes
foreach ($dirs as $dir) {
$file = $dir . DIRECTORY_SEPARATOR . $part;
if (is_readable($file)) {
require $file;
return;
}
}
});