Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Zend framework2 如何在模型ZF2中调用getServiceLocator_Zend Framework2 - Fatal编程技术网

Zend framework2 如何在模型ZF2中调用getServiceLocator

Zend framework2 如何在模型ZF2中调用getServiceLocator,zend-framework2,Zend Framework2,我试图在模型方法中调用getServiceLocator,但它抛出以下错误 致命错误:对非对象调用成员函数get() 实际上,我正在使用$this->getServiceLocator()->get('Config');在导致上述致命错误的模型中,不应在模型类中使用ServiceLocator。请参阅以下答案:,或者在google中搜索“service locator antipattern”,以获取大量反对此做法的论据。您不能在模型类中直接使用ServiceLocator,但如果您想使用$th

我试图在模型方法中调用getServiceLocator,但它抛出以下错误 致命错误:对非对象调用成员函数get()


实际上,我正在使用$this->getServiceLocator()->get('Config');在导致上述致命错误的模型中,不应在模型类中使用ServiceLocator。请参阅以下答案:,或者在google中搜索“service locator antipattern”,以获取大量反对此做法的论据。

您不能在模型类中直接使用ServiceLocator,但如果您想使用
$this->getServiceLocator()->get('Config')在模型上,然后

$config=$this->getServiceLocator()->get('config')在控制器中分配并在madel中传递$config

$ModelTable->searchList($data, $config);

现在您可以调用/使用config的任何方法/函数。

该错误表示您试图在非对象上执行函数。您还必须显示代码,以便我们能够说更多的话。我知道我可以这样做,但我不想遵循这种方法。查看内置的内容,以便直接在模型中使用getServiceLocator。