Chrstopher
Chrstopher

Reputation: 59

How to require a package installed via Composer

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

Upvotes: 3

Views: 398

Answers (3)

srimaln91
srimaln91

Reputation: 1336

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.

Upvotes: 2

sebwas
sebwas

Reputation: 99

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.

Upvotes: 1

Jakumi
Jakumi

Reputation: 8374

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.

Upvotes: 1

Related Questions