PHP Fatal error: Class 'External\\xBBCode\\Xbbcode\\Xbbcode' not found

PHP Fatal error: Class 'External\\xBBCode\\Xbbcode\\Xbbcode' not found

我在 PHP 命名空间方面遇到了真正的麻烦 =_=

所以,项目位置是/home/hosting//htdocs/

文件 /home/hosting//htdocs/Components/News.php 就像:

<?php

namespace Components;

use \External\xBBCode\Xbbcode as Xbbcode;

class News extends \Components\ComponentHelper
{
    const NEWS_DEFAULT_LIMIT = 39; // news per page
    const NEWS_ON_PAGE_LIMIT = 10; // news per page

    public function __construct()
    {
        return true;
    }

    public static function desc_sort ($a, $b)
    {
        return (int)$a < (int)$b;
    }

    public static function Run($_config)
    {
        /*
            data for routing
        */
        $routeData = $_config['route_data'];
        $routeDataCount = count($routeData);

        $newsData = false;
        $newsList = false;
        $db = new \Engine\DB($_config);
        $tpl = new \Engine\Template\Engine($_config);

        $parser = new Xbbcode\Xbbcode();

        $newsYear = 0;
        $newsMonth = 0;
        $newsID = 0;
        $newsFriendlyTitle = '';
        $template = '';

我需要的文件:

/home/hosting/<proj_title>/htdocs/External/xBBCode/Xbbcode/Xbbcode.php
/home/hosting/<proj_title>/htdocs/External/xBBCode/Xbbcode/Attributes.php
/home/hosting/<proj_title>/htdocs/External/xBBCode/Xbbcode/Tag/
/home/hosting/<proj_title>/htdocs/External/xBBCode/Xbbcode/resources/

我的字符串有误

$parser = new Xbbcode\Xbbcode();

但是文件

/home/hosting/<proj_title>/htdocs/External/xBBCode/Xbbcode/Xbbcode.php

存在并开始于:

<?php

namespace Xbbcode;

use Xbbcode\Tag\Tag;


/**
 * Class Xbbcode
 */
class Xbbcode
{

而且我用的是自己的autoloader,放在htdocs目录下,代码:

<?php
/**
 * Файл с автозагрузчиком файлов
 * 
 * PHP Version 5
 * 
 * @category Class
 */

/**
 * Автозагрузчик классов
 * 
 * @category Class
 */
class Autoloader
{
    private static $_loadFile;

    /**
     * Загрузчик
     * 
     * @param string $className - имя класса
     * 
     * @return resuorce
     */
    public static function loadPackages($className)
    {
        $pathParts = explode('\', $className);
        self::$_loadFile = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $pathParts) . '.php';

        if (file_exists(self::$_loadFile) && is_readable(self::$_loadFile)) {
            require_once self::$_loadFile;
        } else {
            throw new Exception('File "' . self::$_loadFile . '" is not found!');
        }
    }

    /**
     * Загрузчик с логированием
     * 
     * @param unknown $className - имя класса
     * 
     * @return resuorce
     */
    public static function loadPackagesAndLog($className)
    {
        try {
            self::loadPackages($className);
            printf("<p>%s: class %s was loaded from %s</p>\n", __METHOD__, $className, self::$_loadFile);
        } catch (Exception $e) {
            printf("<p>%s return exception: %s</p>\n", __METHOD__, $e->getMessage());
        }
    }
}

spl_autoload_register(array('Autoloader', 'loadPackages'));
// spl_autoload_register(array('Autoloader', 'loadPackagesAndLog'));

?>

请有人说,我做错了什么。

您正在正确加载 class 文件,但它存在于 Xbbcode 命名空间中。这是不正确的。应该是(在Xbbcode.php):

namespace External\xBBCode\Xbbcode;

class Xbbcode {
    ...

通常,您希望命名空间代表 class 在项目文件中的位置。