我是 PHP 自动加载的新手,并且在我的最新项目中需要 Horde_Text_Diff。我正在使用 Horde_Autoloader 自动加载所需的文件,但是,我没有正确使用它。据我所知,互联网上没有一个关于如何实际做到这一点的例子。我几乎 100% 从示例中学习,所以我在这里遇到了障碍。
这是我到目前为止...
require_once( Horder/Autoloader.php );
$autoloader = new Horde_Autoloader();
目前没问题,对象已经创建...
$text_diff = $autoloader->loadClass( 'Hoard_Text_Diff' );
这不起作用,因为我在这里纯粹是在猜测。
是什么让我走到了现在的位置 this post 。
最佳答案
我看过 https://github.com/dereuromark/tools/tree/master/Vendor/Horde 的源代码。Horde_Autoloader
没有附加映射器,您使用错误。自动加载器需要添加一个 classPathMapper。 Horde / Autoloader / ClassPathMapper
目录中有不同的 classPathMappers。
require_once 'Horde/Autoloader.php';
require_once 'Horde/Autoloader/ClassPathMapper.php';
require_once 'Horde/Autoloader/ClassPathMapper/Default.php';
$autoloader = new Horde_Autoloader();
$autoloader->addClassPathMapper(new Horde_Autoloader_ClassPathMapper_Default(__DIR__.'PATH_TO_HORDE_FOLDER'));
$autoloader->registerAutoloader();
// if path is correct autoloader should work
$diff = new Horde_Text_Diff();
还有一个默认的自动加载器,它会自动注册 include_path 中的所有路径。这可能有点开销!
// set the current path to your include_path
set_include_path(__DIR__.'PATH_TO_HORDE_FOLDER');
// if you require the default autoloader it will get initialized automatically
require_once 'Horde/Autoloader/Default.php';
// use the lib
$diff = new Horde_Text_Diff();
编辑:
这个对我有用。以下代码在
C:\xampp\htdocs\horde\index.php
中。部落库位于子文件夹 lib
中。// this file:
// C:\xampp\htdocs\horde\index.php
// horde folder structure
// C:\xampp\htdocs\horde\lib\Horde\Autoloader
// C:\xampp\htdocs\horde\lib\Horde\Text
require_once __DIR__.'/lib/Horde/Autoloader.php';
require_once __DIR__.'/lib/Horde/Autoloader/ClassPathMapper.php';
require_once __DIR__.'/lib/Horde/Autoloader/ClassPathMapper/Default.php';
$autoloader = new Horde_Autoloader();
$autoloader->addClassPathMapper(new Horde_Autoloader_ClassPathMapper_Default(__DIR__.'/lib'));
$autoloader->registerAutoloader();
$compare = array(
array(
'foo',
'bar',
'foobar'
),
array(
'foo',
'bar',
'foobaz'
),
);
$diff = new Horde_Text_Diff('auto', $compare);
echo '<pre>';
print_r($diff->getDiff());
echo '</pre>';
关于php - Horde Autoloader - 如何使用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18091627/