PHP5 框架:自动加载和操作码缓存 [英] PHP5 Frameworks: Autoloading and Opcode Caching

查看:32
本文介绍了PHP5 框架:自动加载和操作码缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多框架利用 spl_autoload_register() 来动态加载类(即控制器和模型).有几篇关于自动加载和操作码缓存问题的帖子.一个帖子特别有@cletus 的回复,其中引用了@Rasmus 的一些声明,这些声明被证明对于那些使用 APC 作为操作码缓存的人来说是令人讨厌的:

A number of frameworks utilize spl_autoload_register() for dynamically loading classes (i.e. controllers and models). There are a couple of posts on the issue of autoloading and opcode caching. One post in particular has a response by @cletus which references @Rasmus making a number of statements which prove to be unsavoury for those utilizing APC as an opcode cache:

似乎没有任何关于不影响操作码缓存性能的自动加载的可能替代方案的讨论.

There does not appear to be any discussion as to any possible alternatives to autoloading which do not affect opcode cache performance.

有没有办法绕过自动加载的类没有被添加到字节码缓存的事实?

如果没有,是否有其他方法可以动态加载将被缓存的类?

推荐答案

关于这个话题似乎仍然存在混淆,但在大多数情况下,它归结为易用性与性能.

There seems to still be confusion about this topic, however in most cases it comes down to ease vs performance.

一个很好的邮件列表主题是 Zend Frameworks 邮件列表:

A good mailing list thread to read would be this one on Zend Frameworks mailing list:

http://n4.nabble.com/ZF-and-Autoloading-td640085i20.html

现在,相关性就在这里,因为如果您从尚未定义的类,您可能会依赖自动加载来定义它(尽管您也可能依赖于 include),实际上自动加载设施的存在可能鼓励你使用这样的继承.但这不是自动加载这带来了麻烦(参见 Ramus 在一些麻烦事情的例子的博客).所以正确的短语是倾向于依赖自动加载的人倾向于还使用无视编译时绑定的代码".这是看不到的当然,作为自动加载故障,仅仅避免自动加载无济于事有点 - 你还必须重写你的代码,以便编译时绑定可能会发生.它与使用无关例如,使用new"自动加载.

Now, the correlation is here because if you inherit from not-yet-defined class, you might rely on autoload to define it (though you might also rely on include), and actually the presence of the autoload facility may encourage you to use such inheritance. But this is not the autoload which brings trouble (see after Ramus' "it's not just autoload" in the blog for some examples of troublesome things). So the right phrase would be "people which tend to rely on autoload tend also to use code which defies compile-time binding". Which can't be seen as autoload fault, of course, and just avoiding autoload won't help a bit with that - you would also have to rewrite your code so that compile-time binding could happen. And it has nothing to do with uses of autoload with "new", for example.

至于上述影响的放缓 - 即没有编译时绑定 - 代码确实变得有点慢等等代码可能会在一些晦涩的情况下导致操作码缓存出现问题(不是在自动加载情况下 - 但在定义类的情况下内部条件,或者,上帝保佑,创造了不同的定义取决于条件) - 但它几乎与使用无关自己自动加载.然而,放缓的程度似乎被大大夸大了人 - 这没什么(我再说一遍 - NOTHING)比较由于缺少操作码缓存而带来的性能优势磁盘操作和编译阶段.你大概可以组成一个人为的基准,将显示一些重要的放缓,但我不相信任何真正的应用程序甚至会注意到.

As for the slowdown from the effects described above - i.e., absence of the compile-time binding - the code indeed becomes a bit slower and such code can lead in some obscure cases to some trouble to opcode caches (not in the autoload cases - but in cases where classes are defined inside conditions, or, God forbid, different definition is created depending on condition) - but it has next to nothing to do with using autoload by itself. The amount of slowdown, however, seem to be greatly exagerrated by people - it is nothing (and I repeat to be clear - NOTHING) compared to the performance benefit given by the opcode cache due to the absence of the disk operations and compilation stage. You could probably compose an artificial benchmark that would show some significant slowdown, but I do not believe any real application would even notice.

来源: http://n4.nabble.com/ZF-and-Autoloading-td640085i20.html#a640092

这篇关于PHP5 框架:自动加载和操作码缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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