Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Using packages to include external library
  • I'm trying to include 'simple_html_dom.php' (see http://simplehtmldom.sourceforge.net) for use in one of my applications as a 'package'. It's only 1 class, which I've put in a package named 'domparser'

    packages
          domparser
                classes
                      parser.php
                bootstrap.php

    My bootstrap.php file is as follows:

    Autoloader::add_namespace('Domparser', __DIR__.'/classes/');

    Autoloader::add_core_namespace('Domparser');

    Autoloader::add_classes(array(
        'Domparser\\Parser' => __DIR__.'/classes/parser.php',
    ));

    And in my task:

    $html = new \Parser();

    However I'm getting a rather odd error:

    Compile Error - Cannot redeclare file_get_html() (previously declared in C:\Users\.. ..pathToFuel. ..\fuel\packages\domparser\classes\parser.php:70) in PKGPATH/domparser/classes/parser.php on line 85

    Normally I'd just 'require_once' the file, but packages seem to be the done thing with libs in FuelPHP. Where am I going wrong?

    Cheers,

    Paul

  • If I look in that file, it defines a class called "simple_html_dom_node", not "Parser". Did you rename it?
  • it defines a couple of classes, simple_html_dom and _node, as well as a couple of helper functions outwith either class. I guess I should break these into 2 sep. classes and make the helper functions static?
  • HarroHarro
    Accepted Answer
    If it contains multiple classes, you have to define those classes in the bootloader. If you don't, the autoloader will try to load it, causing this type of errors, since it's already loaded.

    Look in the core bootstrap, where that is done as well (for example for exception classes that are defined in the same file as the core class itself).

    That should work, you should not have to break it up in separate files.
  • Ahh gotcha, thanks.

    Did this, added

    namespace Domparser;

    to parser.php and it seems to be fine. Cheers for the help!

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion