Search code examples
phpcomposer-phppackagepackagist

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


Solution

  • 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.