如何配置 composer 使其始终加载给定的类? composer.json 中可以这样配置吗?
我有一个实用程序类需要一直加载,无论正在执行 PHP 代码的哪一部分。
最佳答案
Files
If you want to require certain files explicitly on every request then you can use the 'files' autoloading mechanism. This is useful if your package includes PHP functions that cannot be autoloaded by PHP.
Example:
{ "autoload": { "files": ["src/MyLibrary/functions.php"] } }
但实际上,这样做没有任何好处。在使用前自动加载类或在使用时自动加载类。如果您在每个请求上都使用该类,那是一样的。我建议使用普通的自动加载方法,因为这意味着当您创建不需要该类的内容时不会加载该类。
关于PHP Composer 总是加载一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18670767/