Why use the Service Manager in Zend Framework 2?

后端 未结 4 2043
日久生厌
日久生厌 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:57

    I think the second approach is better, which is making the controller class independent to the GreetingService. This approach will be useful when you want to use another greeting service to be consumed by the controller. You don't need to change the code in controller class at all, instead you do that by changing the service manager with that factory closure.

    I believe this is the main idea of inversion of control or dependency injection, all wiring and configuration are done outside the class (in this case: controller class).

    So, in my opinion, that's the reason why the second approach is the better one.

提交回复
热议问题