douwei9973 2011-10-27 11:01
浏览 44
已采纳

Zend Framework 2依赖注入混淆

I'm a little confused on how DI works with ZF2. I've spent the last couple of days trying to get my head around it. While I have made some progress a lot of it still baffles me...

Using this (http://akrabat.com/getting-started-with-zend-framework-2/) tutorial I managed to get a grasp that the following:

'di' => array('instance' => array(
        'alias' => array(
            'album' => 'Album\Controller\AlbumController',
        ),
        'Album\Controller\AlbumController' => array(
            'parameters' => array(
                'albums' => 'Album\Model\Albums',
            ),
        ),

works because in our Album Controller class we have a setAlbum function. So when the DI class will call that setAlbums function and pass it the 'Album\Model\Albums' class.

Fine get that no problem..

Now let's look at this (which comes in the skeleton application off the zend site)

            'Zend\View\HelperLoader' => array(
            'parameters' => array(
                'map' => array(
                    'url' => 'Application\View\Helper\Url',
                ),
            ),
        ),

Now i would expect there within the Zend\View\HelperLoader (or an inherited class) would contain a setMap() function that the DI class would pass an array. But this appears not to be the case. As I cannot find a setMap anywhere.

My question is first what am I not understanding about the way DI works with the ZF2... But also what does the code above (about zend\view\helper) actually do. I mean what does injecting 'map' => array('url' => 'Application\View\Helper\Url') into the Zend\View\HelperLoader actually do?

Thanks for any help anyone can give. I appreciate it's a beta framework and what answers I may get now not apply in a months time. But this all seems pretty fundamental and i'm just no getting it!

  • 写回答

1条回答 默认 最新

  • douyuefu1372 2011-12-13 09:31
    关注

    The DI configuration of ZF2 works indeed with the names of the arguments in the signature. It does not matter if this is done with a constructor or a explicit setter. The setter must, however, start with "set" to be recognized by Zend\Di\Di.

    So if you have a class like this:

    <?php
    
    namespace Foo;
    
    class Bar
    {
        public function __construct ($baz) {}
        public function setSomethingElse ($bat) {}
    }
    

    You can inject both a $baz and a $bat:

    'di' => array(
        'instance' => array(
            'Foo\Bar' => array(
                'parameters' => array(
                    'baz' => 'Something\Here',
                    'bat' => 'Something\There',
                ),
            ),
        ),
    )
    

    For Zend\Di it does not matter what the function name exactly is, as long as it starts with "set" and the name of the argument is correct. That is why Foo\Bar::setSomethingElse($bat) works just like Foo\Bar::setBat($bat).

    Just make sure you name your arguments correctly. For example, it is easy to do something like this:

    <?php
    
    namespace Foo;
    
    class Bar
    {
        public function setCacheForBar ($cache) {}
        public function setCacheForBaz ($cache) {}
    }
    

    But that will not work nicely together with Zend\Di.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计