PHP5框架:自动加载和操作码缓存

许多框架利用spl_autoload_register()来动态加载类(即控制器和模型).关于自动加载和操作码缓存的问题有几篇文章.特别是一个帖子有@cletus的响应,它引用了@Rasmus做了一些声明,这些声明对于那些利用APC作为操作码缓存的人来说是不利的:

> Do PHP opcode cache work with __autoload?

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

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

如果没有,是否有任何替代方法可以动态加载将被缓存的类?

解决方法:

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

Zend Frameworks邮件列表上有一个很好的邮件列表线程:

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

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.

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.

source: 07001

上一篇:java – 有没有像spring这样的android应用程序框架?


下一篇:php – 未找到Laravel 5.1路由对象