6

I am trying to add an external library (PHP Simple DOM Parser, http://simplehtmldom.sourceforge.net/index.htm) to a Symfony2 project. I took a tutorial that explains how to include third party libraries to Symfony2 http://www.kiwwito.com/article/add-third-party-libraries-to-symfony-2.

I set up a class file like:

# vendor/phpsimpledom/lib/Phpsimpledom/simple_html_dom.php

require_once __DIR__.'/src/simple_html_dom.php';

class Phpsimpledom_Phpsimpledom extends simple_html_dom_node {
}

and registered my class in my Autoloader (autoload.php):

$loader->registerNamespaces(array(
...
'Phpsimpledom' => __DIR__.'/../vendor/phpsimpledom/lib/',
...
),));

I am trying to call:

$phpsimpledom = new \Phpsimpledom();

but this throughs me an error (Fatal error: Class 'simple_html_dom_node' not found).

However: The main file of the library (simple_html_dom.php) consists of functions that do not belong to a class.

When I try to use the file directly, it also doesn't work:

    $loader->registerNamespaces(array(
...
'Phpsimpledom' => __DIR__.'/../vendor/phpsimpledom/lib/Phpsimpledom/src/simple_html_dom.php',
...
),));

Any hints?

THANKS!

Mike
  • 2,686
  • 7
  • 44
  • 61

2 Answers2

8

You're trying to register a namespace but your class has no namespace. Try adding a namespace to it or use RegisterPrefixes().

BTW: did you know that one of the Symfony components is basically doing the same thing as php simpledom? It's called DomCrawler and it has a support for both xpath and CSS selectors.

Jakub Zalas
  • 35,761
  • 9
  • 93
  • 125
2

I'm new to Symfony2 but as i can see, you are not respecting the PSR for autoloader.

I'm presumable thinking you should do:

# /vendor/phpsimpledom/lib/Phpsimpledom/Phpsimpledom.php

require_once __DIR__.'/src/simple_html_dom.php';

class Phpsimpledom_Phpsimpledom extends simple_html_dom_node {

}

Note that the correct filename would be /vendor/phpsimpledom/lib/Phpsimpledom/Phpsimpledom.php as the call must include the namespace to work.

Hope it works now.