【发布时间】:2014-11-04 18:23:57
【问题描述】:
我有一堂课:ClassA(),我需要教义服务$this->getServiceLocator()->get('Doctrine\ORM\EntityManager');。
我尝试实现接口ServiceManagerAwareInterface,但函数getServiceLocator() 和setServiceLocator(ServiceLocatorInterface $serviceLocator) 不起作用。
有人在 ZF2 的 Controller 类之外使用了 ServiceLocator?有可能吗?
<?php
namespace DbSession\Service;
use Zend\Session\SaveHandler\SaveHandlerInterface;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorAwareTrait;
/**
* Description of SessionDB
*
* @author rab
*/
class SessionDB implements SaveHandlerInterface, ServiceLocatorAwareInterface {
use ServiceLocatorAwareTrait;
/**
* Session Save Path
*
* @var string
*/
protected $sessionSavePath;
/**
* Session Name
*
* @var string
*/
protected $sessionName;
/**
* Lifetime
* @var int
*/
protected $lifetime;
/**
* Constructor
*
*/
public function __construct() {
}
/**
* Open the session
*
* @return bool
*/
public function open($savePath, $name) {
echo "open session";
}
/**
* Close the session
*
* @return bool
*/
public function close() {
echo "close session";
}
/**
* Read the session
*
* @param int session id
* @return string string of the sessoin
*/
public function read($id) {
echo "read session";
}
/**
* Write the session
*
* @param int session id
* @param string data of the session
*/
public function write($id, $data) {
$this->getServiceLocator()->get('');
echo "write";
}
/**
* Destoroy the session
*
* @param int session id
* @return bool
*/
public function destroy($id) {
echo "destroy session";
}
/**
* Garbage Collector
*
* @param int life time (sec.)
* @return bool
*/
public function gc($maxlifetime) {
echo "gc session";
}
}
【问题讨论】:
-
你在扩展任何类吗?你有什么错误?什么不工作?电脑会起火吗?>
-
@Kisaragi 我的班级实现了另一个接口:
SaveHandlerInterface。我的班级:班级SessionDatabase implementsSaveHandlerInterface。我没有 PHP 错误,当我实现ServiceManagerAwareInterface时,函数getServiceLocator()和setServiceLocator(ServiceLocatorInterface $serviceLocator)没有运行
标签: php doctrine-orm zend-framework2 service-locator