serviceLocator为null

时间:2016-08-13 02:15:33

标签: php zend-framework2 zend-servicemanager

class CustomSawException extends Exception implements ServiceLocatorAwareInterface, SawExceptionInterface
{
    protected $serviceLocator;

    public function test(){
        $this->serviceLocator->get('SomeThing');
    }

    /**
     * Set service locator
     *
     * @param ServiceLocatorInterface $serviceLocator
     */
    public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
    {
        $this->serviceLocator = $serviceLocator;
    }

    /**
     * Get service locator
     *
     * @return ServiceLocatorInterface
     */
    public function getServiceLocator()
    {
        return $this->serviceLocator;
    }

为此抛出的异常是:

  

调用get()on null

我无法弄清楚它为什么会抛出异常? ServiceLocatorAwareInterface应该注入ServiceLocator

1 个答案:

答案 0 :(得分:1)

  

ServiceLocator应该注入new { pt.Id, pt.Title }

我不太确定,在最新版本的ZF2中,这个界面已被弃用。

另请阅读this post on GitHubthis stackoverflow question了解详情。