我的页面的文件夹
system/ <-- offline files (classes, cronjobs, libraries ...)
online/ <-- contain all php scripts ( available online )
system/
classes <-- all my own classes inside.
... directories ...
libraries/htmlpurifier <- contain HTMLPurifier
我想使用spl_autoload_register()
<?php
define("DOC_ROOT", dirname(__DIR__));
function hTMLPurifier($class) {
require DOC_ROOT . '/system/libraries/htmlpurifier/library/' . strtolower(str_replace('\\', '/', $class)) . '.php';
}
function myAutoload($class) {
require DOC_ROOT . '/' . strtolower(str_replace('\\', '/', $class)) . '.php';
}
spl_autoload_register('hTMLPurifier');
spl_autoload_register('myAutoload');
$config = HTMLPurifier_Config::createDefault();
$purifier = new HTMLPurifier($config);
$clean_html = $purifier->purify($dirty_html);
?>
我收到关注消息。
.../htmlpurifier_config.php): failed to open stream: No such file or directory in ...
如果插入
"require_once '/path/to/HTMLPurifier.auto.php';"
在顶部
<?php
define("DOC_ROOT", dirname(__DIR__));
require_once '/path/to/HTMLPurifier.auto.php';
function myAutoload($class) {
require DOC_ROOT . '/' . strtolower(str_replace('\\', '/', $class)) . '.php';
}
spl_autoload_register('myAutoload');
$config = HTMLPurifier_Config::createDefault();
$purifier = new HTMLPurifier($config);
$clean_html = $purifier->purify($dirty_html);
?>
一切正常。
我只想使用 spl_autoload_register()加载所有类 没有插入
"require_once '/path/to/HTMLPurifier.auto.php';"
在顶部。 我该怎么办??
答案 0 :(得分:0)
HTML Purifier不适用于PHP名称空间,因此将\\
替换为/
不会使您获得想要的结果。您的自动加载逻辑试图从文件HTMLPurifier_Config
加载类htmlpurifier_config.php
-因此,您需要的是这样的东西:
function autoloadHtmlPurifier($class) {
require DOC_ROOT . '/system/libraries/htmlpurifier/library/'
. str_replace('_', '/', $class) . '.php';
}
spl_autoload_register('autoloadHtmlPurifier');
(请注意,此函数不使用strtolower()
并将_
替换为/
。)
但是,看着the HTML Purifier autoload implementation:
public static function getPath($class)
{
if (strncmp('HTMLPurifier', $class, 12) !== 0) {
return false;
}
// Custom implementations
if (strncmp('HTMLPurifier_Language_', $class, 22) === 0) {
$code = str_replace('_', '-', substr($class, 22));
$file = 'HTMLPurifier/Language/classes/' . $code . '.php';
} else {
$file = str_replace('_', '/', $class) . '.php';
}
if (!file_exists(HTMLPURIFIER_PREFIX . '/' . $file)) {
return false;
}
return $file;
}
...由于语言类的边缘情况,很可能简单替换仍然无法正常工作。但您可以尝试以下方法:
function autoloadHtmlPurifier($class) {
require DOC_ROOT . '/system/libraries/htmlpurifier/library/'
. str_replace('_', '/', $class) . '.php';
}
spl_autoload_register('autoloadHtmlPurifier');
spl_autoload_register(array('HTMLPurifier_Bootstrap', 'autoload'));
它使用您的自定义自动加载器来加载HTMLPurifier_Bootstrap
类,因此您无需将其包含在require
中,但仍具有将规范的自动加载器用于HTML Purifier的优点。
也就是说,无论哪种情况,不利之处在于,如果HTML Purifier确实切换到了名称空间,则它的自动加载器会损坏。我真的建议您坚持使用require_once(DOC_ROOT . '/system/libraries/htmlpurifier/library/HTMLPurifier.auto.php')
方法,或使用composer
安装HTML Purifier,这将take care of autoloading that library (and possibly others) for you:
对于指定自动加载信息的库,Composer会生成一个vendor / autoload.php文件。您只需添加此文件即可开始使用这些库提供的类,而无需进行任何额外的工作:
require __DIR__ . '/vendor/autoload.php';