phpautoloaderhorde

Horde Autoloader - How to use?


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.


Solution

  • 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>';