3

I am new to autoloading in PHP and have a requirement for Horde_Text_Diff in my latest project. I am using Horde_Autoloader to autoload the required files, however, I am not using it correctly. As far as I can Google, there is not a single example of how to actually do it that exists on the internet. I pretty much learn 100% from example and so I have hit a roadblock here.

This is what I have so far ...

require_once( Horder/Autoloader.php );
$autoloader = new Horde_Autoloader();

No problem so far, the object is created ...

$text_diff = $autoloader->loadClass( 'Hoard_Text_Diff' );

This is not working as I am purely guessing here.

What lead me to where I am was this post.

Community
  • 1
  • 1
James Huckabone
  • 615
  • 1
  • 12
  • 32

1 Answers1

3

I've looked at the source code at https://github.com/dereuromark/tools/tree/master/Vendor/Horde.

The Horde_Autoloader does not have a mapper attached and you're using it wrong. The autoloader needs a classPathMapper added. There are different classPathMappers in the Horde / Autoloader / ClassPathMapper directory.

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();

There is also a default autoloader which registers all paths from your include_path automatically. This can be a little overhead!

// 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();

EDIT:

It works for me. The following code is in C:\xampp\htdocs\horde\index.php. The horde lib is in subfolder 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>';
bitWorking
  • 12,485
  • 1
  • 32
  • 38
  • Except that it didn't work, my bad! When I var_dump $autoload, it shows the following: object(Horde_Autoloader)#3 (2) { ["_mappers":"Horde_Autoloader":private]=> array(1) { [0]=> object(Horde_Autoloader_ClassPathMapper_Default)#2 (1) { ["_includePath":"Horde_Autoloader_ClassPathMapper_Default":private]=> string(21) "/usr/share/pear/Horde" } } ["_callbacks":"Horde_Autoloader":private]=> array(0) { } } – James Huckabone Aug 07 '13 at 00:19
  • Do I need to call loadClass? – James Huckabone Aug 07 '13 at 00:21
  • No. An autoloader is supposed to allow you to simply use any class you need by NOT explicitly call code that loads the class. Such code that is unnecessary is both any call to a method that loads the class, as well as `require()`. – Sven Aug 07 '13 at 06:54
  • @JamesHuckabone the var_dump looks good. do you get any errors (turn on error_reporting) if you try to use `Horde_Text_Diff`. Also did you try both examples that I posted? – bitWorking Aug 07 '13 at 13:36
  • Yes, I tried both and get "Horde_Text_Diff" not found on both. – James Huckabone Aug 07 '13 at 15:43
  • @JamesHuckabone then the path seems to be wrong. try to add the path `/usr/share/pear` not `/usr/share/pear/Horde`. – bitWorking Aug 07 '13 at 16:12
  • When I change the path, the not found class is "Horde_Text_Diff_Engine_" ... do I have to add another path or something? – James Huckabone Aug 07 '13 at 16:20
  • After a few more tries I finally realized it was a permissions issue. Autoloader installed with permissions that were cool, text diff did not. It works now! Thanks again!!! – James Huckabone Aug 07 '13 at 23:10