Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add external libraries to Symfony2 project

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!

like image 758
Mike Avatar asked Jan 09 '12 14:01

Mike


1 Answers

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.

like image 155
Jakub Zalas Avatar answered Sep 28 '22 08:09

Jakub Zalas