Why use the Service Manager in Zend Framework 2?

后端 未结 4 2041
日久生厌
日久生厌 2021-02-02 01:26

lets say i have a service:

namespace Helloworld\\Service;

class GreetingService
{
    public function getGreeting()
    {
        if(date(\"H\") <= 11)
              


        
4条回答
  •  北荒
    北荒 (楼主)
    2021-02-02 01:59

    To add something to @yechabbis answer:

    The factory pattern for the ServiceConfiguration is indeed for complex infrastructure or simply to not use closures / callable functions inside the configuration. This is for two reasons:

    • Readability / Cleaner Code
    • Performance

    Setting up the factory pattern inside getServiceConfig is nice, clean and fast. No classes are instantiated but will so upon call of the service-key. If, however, you set up services using the closure pattern or callable function, then those classes will always be instantiated, upon every single request!

    This probably is just an example thing, but the code you've shown would also best be served as a ViewHelper

提交回复
热议问题