Search code examples
phpnamespacesautoloader

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


I have real troubles with PHP namespaces =_=

So, project place is /home/hosting//htdocs/

File /home/hosting//htdocs/Components/News.php is like:

<?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 = '';

And I have needed files:

/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/

I have an error in string

$parser = new Xbbcode\Xbbcode();

But file

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

is exist and start like:

<?php

namespace Xbbcode;

use Xbbcode\Tag\Tag;


/**
 * Class Xbbcode
 */
class Xbbcode
{

And I use own autoloader, which placed in htdocs dir, code of it:

<?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'));

?>

Please someone say, what I do wrong.


Solution

  • You're loading the class file correctly, but it exists in the Xbbcode namespace. This is incorrect. It should be (in Xbbcode.php):

    namespace External\xBBCode\Xbbcode;
    
    class Xbbcode {
        ...
    

    Generally, you want your namespaces to represent where in your project files that the class lies.