Zend Framework 2中的工厂类与闭包 [英] Factory classes vs closures in Zend Framework 2

查看:76
本文介绍了Zend Framework 2中的工厂类与闭包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Zend Framework 2中使用工厂类或闭包是否更好?为什么?

我知道闭包无法序列化,但是如果您从Module#getServiceConfig()返回它们,则不会影响其余配置数据的缓存,并且无论如何,闭包都将被缓存在您的操作码缓存中. /p>

在构造工厂类和执行闭包方面,性能有何不同? PHP是仅在执行闭包时才对其进行包装和实例化,还是针对每个请求对配置文件中定义的每个闭包都进行封装和实例化?

有没有人比较每种方法的执行时间?

另请参阅:

解决方案

PHP会在编译时将配置中的匿名函数转换为闭包类的实例,因此它将对每个请求执行此操作.这与create_function不同,后者会在运行时创建函数.但是,由于闭包是在编译时执行的,因此它应该在您的opcache缓存中,因此没有关系.

首先,关于使用工厂vs闭包构造服务的性能影响,您必须记住,无论请求多少次,每个请求只能构造一次服务.我运行了一个使用闭包和工厂来获取服务的快速基准测试,这就是我所得到的(我运行了几次,所有结果都具有相同的值):

Closure: 0.026999999999999ns
Factory: 0.30200000000002ns

那是纳秒,即10 -9 秒.基本上,性能差异很小,因此没有有效的差异.

此外,ZF2无法使用闭包来缓存我整个模块的配置.如果仅使用工厂,则可以合并,缓存我的整个配置,并且可以在每个请求上读取一个简单的文件,而不必担心每次都加载和合并配置文件.我还没有衡量这对性能的影响,但是我想无论如何它都是微不足道的.

但是,我更喜欢工厂,主要是为了提高可读性和可维护性.对于工厂,您最终不会得到一些庞大的配置文件,到处都是大量的关闭.

当然,闭包非常适合快速开发,但是如果您希望代码可读易维护,那么我要坚持使用工厂.

Is it better to use factory classes or closures in Zend Framework 2, and why?

I know that closures cannot be serialized, but if you return them from Module#getServiceConfig(), this will not affect the caching of the rest of your configuration data, and the closures would be cached in your opcode cache anyway.

How does performance differ in constructing a factory class vs executing a closure? Does PHP wrap and instantiate closures only when you execute them, or would it do this for every closure defined in your configuration file on every request?

Has anyone compared the execution times of each method?

See also:

解决方案

PHP will convert the anonymous functions in your configuration to instances of the closure class at compile time so it would do this on every request. This is unlike create_function which will create the function at run time. However, since closures do this at compile time it should be in your opcache cache and therefore should not matter.

In terms of the performance impact of constructing a service using a factory vs closure firstly you have to remember that the service will only ever be constructed once per request regardless of how many times you ask for the service. I ran a quick benchmark of fetching a service using a closure and a factory and here is what I got (I ran a few times and all of the results were about the same value):

Closure: 0.026999999999999ns
Factory: 0.30200000000002ns

Those are nanoseconds, i.e 10-9 seconds. Basically the performance difference is so small that there is no effective difference.

Also ZF2 can't cache my whole module's configuration with closures. If I use purely factories then my whole configuration can be merged, cached and a simple file can be read on each request rather than having to worry about loading and merging configuration files every time. I've not measured the performance impact of this, but I'd guess it is marginal in any case.

However, I prefer factories for mainly readability and maintainability. With factories you don't end up with some massive configuration file with tons of closures all over the place.

Sure, closures are great for rapid development but if you want your code to be readable and maintainable then I'd say stick with factories.

这篇关于Zend Framework 2中的工厂类与闭包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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