gpt4 book ai didi

php - Horde Autoloader - 如何使用?

转载 作者:行者123 更新时间:2023-12-04 23:53:13 24 4
gpt4 key购买 nike

我是 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。 horde lib 位于子文件夹 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com