3

I installed emanueleminotto/simple-html-dom via composer.

How can I use classes from the package without getting an error?

Note: I use XAMPP to run PHP scripts.

Error Message:

PHP Fatal error: Uncaught Error: Class 'simple_html_dom' not found in C:\xampp\htdocs\Practice\PHP\scrape_1.php:3 Stack trace:

0 {main}

thrown in C:\xampp\htdocs\Practice\PHP\scrape_1.php on line 3

Fatal error: Uncaught Error: Class 'simple_html_dom' not found in C:\xampp\htdocs\Practice\PHP\scrape_1.php:3 Stack trace:

0 {main}

thrown in C:\xampp\htdocs\Practice\PHP\scrape_1.php on line 3

Chrstopher
  • 59
  • 1
  • 8

3 Answers3

2

After running

$ composer install

require the autoloader generated in vendor/autoload.php at the top of your script file (or, for a web application, in the front controller).

Then you will have all autoloaded classes available in your script.

<?php    

require_once __DIR__ . '/vendor/autoload.php';

$htmlDom = new simple_html_dom_node();

For reference, see https://getcomposer.org/doc/01-basic-usage.md#autoloading.

localheinz
  • 9,179
  • 2
  • 33
  • 44
srimaln91
  • 1,176
  • 10
  • 21
1

apparently emanueleminotto/simple-html-dom doesn't use a namespace so by default uses the global namespace. the clean solution would be to include the vendor/autoload.php (created/generated/updated by composer) and use the classes/functions by prepending \, to indicate the global namespace ... unless you work in the global namespace yourself, in which case you don't have to prepend.

Jakumi
  • 8,043
  • 2
  • 15
  • 32
1

You should be able to just use them. If I see that right, the whole package is really only one file which is autoloader by composer.

If you include the vendor/autoload.php file in your PHP Script, you should be good to go with the classes in the package.

sebwas
  • 99
  • 1
  • 8