Factory classes vs closures in Zend Framework 2
Asked Answered
N

1

8

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:

Nighttime answered 20/10, 2013 at 15:26 Comment(0)
D
11

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.

Dalenedalenna answered 21/10, 2013 at 0:0 Comment(3)
About the caching thing: Everything with closures should go inside getXyzConfig() of your Module-Class. Only non-closure-config inside module.config.php ;)Tina
Not to mention that closures all are created on every request, but factory classes are only instantiated when the service behind them is actually required.Lucky
+1 Thanks; A belated note from me is that you can also create factory base classes for similar services which allows you to reuse existing factory code via inheritance. Another plus for your "readability and maintainability" point.Thing

© 2022 - 2024 — McMap. All rights reserved.