关于ServiceLocator ,AdpaterAwareInterface 注入

今天学习zf2 的过程,视频中讲到要把数据库的中的表继承TableGateway中,然后注册在ServiceManager中,但是里面没有$adapter,需要在serviceManger,中配置initializer,添一个类,这个类实现intializerInterface,里面有个initialize方法,判断如果服务产生的类实现了AdapterInterface,则执行setDbAdapter(),将SerivcieLocator->get('adpater')注入到该类,这样就实现在调用该userTable服务时,自动注入了$adpater.

然后,我想的是如果userTable直接实现serviceLocatorAwareInterface不是也可把adater注入进来吗,于是,试了一下,serviceLocator能进来,但是该服务在是要直接产生实例的,没办法把serviceLocator给$adapter,所以还得实现AdapterInterface,初始化把它注入进去

原文地址:https://www.cnblogs.com/musings/p/5527899.html