简体   繁体   English

如何要求通过Composer安装包

[英]How to require a package installed via Composer

I installed emanueleminotto/simple-html-dom via composer . 我通过composer安装了emanueleminotto/simple-html-dom

How can I use classes from the package without getting an error? 如何在不收到错误的情况下使用包中的类?

Note : I use XAMPP to run PHP scripts. 注意 :我使用XAMPP来运行PHP脚本。

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: PHP致命错误:未捕获错误:在C:\\ xampp \\ htdocs \\ Practice \\ PHP \\ scrape_1.php中找不到类'simple_html_dom':3堆栈跟踪:

0 {main} 0 {主}

thrown in C:\\xampp\\htdocs\\Practice\\PHP\\scrape_1.php on line 3 在第3行的C:\\ xampp \\ htdocs \\ Practice \\ PHP \\ scrape_1.php中抛出

Fatal error: Uncaught Error: Class 'simple_html_dom' not found in C:\\xampp\\htdocs\\Practice\\PHP\\scrape_1.php:3 Stack trace: 致命错误:未捕获错误:在C:\\ xampp \\ htdocs \\ Practice \\ PHP \\ scrape_1.php中找不到类'simple_html_dom':3堆栈跟踪:

0 {main} 0 {主}

thrown in C:\\xampp\\htdocs\\Practice\\PHP\\scrape_1.php on line 3 在第3行的C:\\ xampp \\ htdocs \\ Practice \\ PHP \\ scrape_1.php中抛出

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). 要求在脚本文件顶部的vendor/autoload.php中生成vendor/autoload.php (或者,对于Web应用程序,在前端控制器中)。

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 . 有关参考,请参阅https://getcomposer.org/doc/01-basic-usage.md#autoloading

apparently emanueleminotto/simple-html-dom doesn't use a namespace so by default uses the global namespace. 显然, emanueleminotto/simple-html-dom不使用命名空间,因此默认情况下使用全局命名空间。 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. 干净的解决方案是包括vendor/autoload.php (由composer创建/生成/更新)并使用类/函数通过前置\\来指示全局命名空间...除非你自己在全局命名空间中工作,在哪种情况下你不必前置。

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. 如果在PHP脚本中包含vendor/autoload.php文件,那么最好使用包中的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM