1

我正在测试将我的应用程序从现在使用的 SplAutoloading 迁移到 Symfonies 自动加载器。

我的应用程序结构是

> Components
> Models
    > Core
        Test.php
Index.php

我的 Test.php 是这样的

namespace Core;

class Test {    
}

试图将它加载到我的 index.php

define('__WEB_ROOT__', __DIR__);

//Symfony Class Loader
require_once __WEB_ROOT__ . '/Components/ClassLoader/UniversalClassLoader.php';

use Symfony\Component\ClassLoader\UniversalClassLoader;

$oLoader = new UniversalClassLoader();
$oLoader->registerNamespaces(
array(
    'Core' => __WEB_ROOT__ . '/Models/Core/'
)
);
$oLoader->register();

use Core\Test;

$oTest = new Test();

不知道为什么类不能像这样加载?有什么帮助吗?这里的正确用法是什么

4

1 回答 1

0

设置的文件路径不应包含命名空间名称。因此,如果Core\Test位于 中__WEB_ROOT__/Models/Core/Test.php,您应该将Core命名空间注册到__WEB_ROOT__/Models

$oLoader->registerNamespaces(array(
    'Core' => __WEB_ROOT__ . '/Models/',
));
于 2014-04-12T17:18:49.817 回答