zend-framework - zend framework2和3中getServiceConfig函数的配置问题
phpcn_u1582
phpcn_u1582 2017-05-16 16:43:14
[PHP讨论组]
//Module.php中的一段代码(项目是zend framework2官网上的简单例子)
 public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'Album\Model\AlbumTable' =>  function($sm) {
                    $tableGateway = $sm->get('AlbumTableGateway');
                    $table = new AlbumTable($tableGateway);
                    return $table;
                },
                'AlbumTableGateway' => function ($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Album());
                    return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
                },
            ),
        );
    }
//zend framework3中的样子
 public function getServiceConfig()
    {
        return [
            'factories' => [
                Model\AlbumTable::class => function($container) {
                    $tableGateway = $container->get(Model\AlbumTableGateway::class);
                    return new Model\AlbumTable($tableGateway);
                },
                Model\AlbumTableGateway::class => function ($container) {
                    $dbAdapter = $container->get(AdapterInterface::class);
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Model\Album());
                    return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
                },
            ],
        ];
    }
  1. $sm是什么类型?

  2. Model\AlbumTableGateway::class这个怎么理解?整个项目中并没有出现AlbumTableGateway这个类,只有AlbumTable这个类

phpcn_u1582
phpcn_u1582

全部回复(1)
PHP中文网

自己找了一个相似的问题,原文在下面,翻译在更下面。

source: AlbumTableGateway in Zend Framework 2 User Guide

The best way to think of this is that ServiceManager's get() method takes a key value, not a class name. The key value needs to map to something that will result in a class instance being returned.

If the key is within the invokables section, then the ServiceManager will try to instantiate the string that the key points to on the assumption that it's a classname:

'invokables' => array(
    'some_name' => 'My\Mapper\SomeClassName',
),

If the key is within the factories section, then the ServiceManager will execute the callback that the key points to and expect an object instance to be returned:

'factories' => array(
    'some_name' => function($sm) { return new \My\Mapper\SomeClassName(); },
),

In general, you use a factory when you need to do something more than just instantiate a class - usually you need to set up the class with another dependency. If you just need to instantiate a class, use an invokable.


翻译:
最好这样想:ServiceManagerget()方法接受一个而不是一个类名,这个会去匹配invokablesfactories中的元素并返回一个创建的对象。
如果这个是处于invocables的区域, 它就会实例化匹配到的那个类。

'invokables' => array(
    'some_name' => 'My\Mapper\SomeClassName',
),

如果这个处于工厂里,就会通过指向的callback函数实例化一个对象返回。

'factories' => array(
    'some_name' => function($sm) { return new \My\Mapper\SomeClassName(); },
),

(如果都没匹配到就报错了)
一般来说,只有当你不仅仅是实例化一个已存在的类,而是要去构建一个有其他依赖的类的时候才会使用factories,否则的话就用invokables就好了

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号