找不到类 - 使用"使用"

Tho*_*omK 5 php

在PHP编程缺席4年后,我正在尝试在其中创建一些新项目.

我正在收集一些有用的库.我有"使用"关键字的问题.这是我抛出错误的代码部分.

<?PHP

    use Assetic\Asset\AssetCollection;
    use Assetic\Asset\FileAsset;
    use Assetic\Asset\GlobAsset;

    $js = new AssetCollection(array(
    ...
?>
Run Code Online (Sandbox Code Playgroud)

我收到错误:

Fatal error: Class 'Assetic\Asset\AssetCollection' not found in /home/php/index.php on line 7
Run Code Online (Sandbox Code Playgroud)

我认为php.ini中的include_path可能有问题,但它看起来像这样:

include_path = ".:/usr/share/php5:/usr/share/php"
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

BTW.我正在使用nginx + php-fpm.

Pel*_*off 11

use关键字实际上不包含任何文件.我担心您要么使用spl_register_autoload()调用注册自动加载功能,要么手动包含这些文件.

http://www.php.net/manual/en/function.spl-autoload-register.php

通常,一个好的默认自动加载器会查找与命名空间相同路径的文件,如下所示:

spl_autoload_register(
    function($className)
    {
        $className = str_replace("_", "\\", $className);
        $className = ltrim($className, '\\');
        $fileName = '';
        $namespace = '';
        if ($lastNsPos = strripos($className, '\\'))
        {
            $namespace = substr($className, 0, $lastNsPos);
            $className = substr($className, $lastNsPos + 1);
            $fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
        }
        $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';

        require $fileName;
    }
);
Run Code Online (Sandbox Code Playgroud)

有关PHP中的自动加载的更多信息,这是许多(较新的)项目所遵循的结构:http://groups.google.com/group/php-standards/web/psr-0-final-proposal