我遇到了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
{
我使用自己的自动加载器,它放在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'));
?>
请有人说,我做错了。
答案 0 :(得分:0)
您正确加载了类文件,但它存在于Xbbcode
命名空间中。这是不正确的。它应该是(在Xbbcode.php
中):
namespace External\xBBCode\Xbbcode;
class Xbbcode {
...
通常,您希望命名空间表示项目文件中该类所在的位置。