PHP作曲家始终加载类 [英] PHP composer to always load a class

查看:80
本文介绍了PHP作曲家始终加载类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何配置作曲器,使其始终加载给定的类?可以在composer.json中进行这种配置吗?

How do I configure composer so it always loads a given class? Is such configuration possible in composer.json?

我有一个需要始终加载的实用程序类,而不管要执行PHP代码的哪一部分。

I have a utility class that needs to be loaded all the time, regardless of what part of the PHP code is being executed.

推荐答案


文件



想要在每个请求上明确要求某些文件,则可以使用文件自动加载机制。如果您的软件包中包含无法由PHP自动加载的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.

示例:

{
   "autoload": {
       "files": ["src/MyLibrary/functions.php"]
    }
}

- http://getcomposer.org/doc/04-schema.md#files






但实际上,这样做没有任何好处。在使用前自动加载类,或在使用时自动加载类。如果您在每个请求上都使用课程,则该课程是相同的。我建议使用常规的自动加载方法,因为这意味着在创建不需要该类的内容时不会加载该类。


But really, there is no benefit of doing this. Autoloading a class before you use it or autoloading your class when you use it. If you use the class on each request, it's the same. I recommend using the normal autoloading methods, because that means the class isn't loaded when you create something that doesn't need that class.

这篇关于PHP作曲家始终加载类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆