延迟加载?是不是最好避免呢?
我刚刚读到“延迟加载”设计模式。
过度使用延迟加载来加载所有类并完全忘记 include(..)
是否可以?
这种方法有什么缺点?
I just read about the "lazy loading" design pattern.
Is it ok to overuse lazy loading to load all classes and forget about include(..)
entirely?
What are the cons of this approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
关于自动加载的几点:
您将看到良好的性能
通过使用自动加载进行改进
与总是包括你所有的
一直有文件
(尤其是文件数量
变得越来越大)。
实现自动加载时,是
更好用
spl_autoload_register()
比__autoload()
.尽管很多时候,当人们谈论 PHP 中的延迟加载时,他们谈论的是类似以下内容:
然后,您仅在实际需要使用属性时才加载该属性,而不是每次实例化该对象时都加载该属性,这可能会产生潜在的问题。一些好的好处。
A couple points on autoloading:
You will see a nice performance
improvement by using autoloading
versus always including all of your
files all the time
(especially as the number of files
grows larger and larger).
When implementing autoloading, it is
better to use
spl_autoload_register()
than__autoload()
.Although a lot of times when people talk about lazy loading in PHP, they are talking about something like the following:
Then you only load a property when it actually needs to be used, and not every time you instantiate the object, which can potentially have some good benefits.
惰性加载器的好处之一是它只加载脚本在执行过程中实际需要的类文件,从而可能节省内存;否则,您可能会包含所有类文件,无论是否需要它们。根据您的脚本,这可能会产生很大的不同。
One benefit of a lazy loader is that it only loads the class files that are actually needed by the script during the course of its execution, potentially saving memory; when otherwise you might include all class files, whether they are needed or not. Depending on your scripts, this can make quite a difference.
可以使用显式包含,或者让
__autoload()
为您找到您的类。无论哪种方式。不过,我不建议混合使用这两种策略。
include
行是不必要的。It is fine to use explicit includes, or to have
__autoload()
find your classes for you. Either way.I wouldn't recommend mixing the two strategies, though. The
include
lines would be unnecessary.