问题描述
我一直在纠结如何最好地将类包含到我的 php 代码中.路径通常是一个问题,但几分钟前我发现 这个问题极大地帮助了这一点.现在我正在阅读 __autoload 并认为它可以使开发我的应用程序的过程变得更加容易.问题是我喜欢维护文件夹结构以分隔功能区域,而不是将所有内容都放入通用/lib 文件夹中.因此,如果我覆盖自动加载以对包含所有子文件夹的类文件夹进行深入搜索,我可以期待什么性能影响?
I've always struggled with how to best include classes into my php code. Pathing is usually an issue but a few minutes ago i found this question which dramatically helps that. Now I'm reading about __autoload and thinking that it could make the process of developing my applications much easier. The problem is i like to maintain folder structure to separate areas of functionality as opposed to throwing everything into a general /lib folder. So if i override autoload to do a deep search of a class folder including all subfolders, what performance hits can i expect?
显然,这取决于规模、文件夹结构的深度和类的数量,但通常我会问中等规模的项目是否会导致问题.
Obviously this will depend on scale, depth of the folder structure and number of classes but generally I'm asking on a medium scale project will it cause problems.
推荐答案
__autoload 很棒,但是在递归搜索函数中声明所有文件的成本很高.您可能想查看构建用于自动加载的文件树.在我的框架中,我始终为它们的类命名文件,并使用为数据缓存的映射.
__autoload is great, but the cost of stating all the files in a recursive search function is expensive. You might want to look at building a tree of files to use for autoloading. In my framework, I consistently name files for their classes and use a map that is cached for the data.
查看 [死链接] 从第 68 行开始,以了解如何可以做到.
Check out [dead link] starting at line 68 for an idea of how this can be done.
为了更直接地回答您的问题,无需缓存,您可以预期在中等至大量流量的网站上性能会受到影响.
And to more directly answer your question, without caching, you can expect a performance hit on a site with medium to heavy traffic.
这篇关于在 php 中使用自动加载并搜索类文件会影响性能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!