PHP实现接口方法时出现致命错误
Note for the zend folks: I am hoping this won't turn out to be a ZF specific question as I can see this is purely related to PHP (or in general an OOP) but added ZF tag just to see if there is something else I am missing
对于zend人员的注意事项:我希望这不会成为特定于ZF的问题,因为我可以看到这与PHP(或通常是OOP)纯粹相关,但添加了ZF标记只是为了查看是否还有其他问题我失踪了
I am creating a project in ZF 2 where I have a class which is implementing an interface called Service aware class Zend\ServiceManager\ServiceLocatorAwareInterface
我在ZF 2中创建了一个项目,我有一个类,它实现了一个名为Service aware class Zend \ ServiceManager \ ServiceLocatorAwareInterface的接口
Where this interface is very simple
这个界面非常简单
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\ServiceManager;
interface ServiceLocatorAwareInterface
{
/**
* Set service locator
*
* @param ServiceLocatorInterface $serviceLocator
*/
public function setServiceLocator(ServiceLocatorInterface $serviceLocator);
/**
* Get service locator
*
* @return ServiceLocatorInterface
*/
public function getServiceLocator();
}
and my implementor class is this (I have removed everything else from this class for the sake of debugging, right now this is full length code in my class)
我的实现者类就是这个(为了调试,我已经从这个类中删除了其他所有东西,现在这是我班上的全长代码)
<?php
namespace FileServer\Service;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\Di\ServiceLocatorInterface;
class Data implements ServiceLocatorAwareInterface{
protected $sl;
public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
{
$this->sl = $serviceLocator;
}
public function getServiceLocator()
{
return $this->sl;
}
}
and I have an include statement in the index.php
我在index.php中有一个include语句
include( 'E:\myproject\module\FileServer\src\FileServer\Service\Data.php' )
which is throwing PHP fatal error. Below is the complete error message
这是抛出PHP致命错误。以下是完整的错误消息
Fatal error: Declaration of FileServer\Service\Data::setServiceLocator() must be compatible with Zend\ServiceManager\ServiceLocatorAwareInterface::setServiceLocator(Zend\ServiceManager\ServiceLocatorInterface $serviceLocator) in E:\myproject\module\FileServer\src\FileServer\Service\Data.php on line 23
致命错误:FileServer \ Service \ Data :: setServiceLocator()的声明必须与E:\ myproject \ module \ FileServer \ src \ FileServer \中的Zend \ ServiceManager \ ServiceLocatorAwareInterface :: setServiceLocator(Zend \ ServiceManager \ ServiceLocatorInterface $ serviceLocator)兼容第23行的Service \ Data.php
As you can see I am implementing both the getter and setter functions with proper signature but still getting the error. What I am missing/overlooking here?
正如您所看到的,我正在使用适当的签名实现getter和setter函数,但仍然会收到错误。我错过了什么/在这里俯瞰?
1 个解决方案
#1
1
Replace:
更换:
use Zend\Di\ServiceLocatorInterface;
With:
附:
use Zend\ServiceManager\ServiceLocatorInterface;
As thats the proper interface the method is expecting. Even though they have the same name, the namespace differs.
因为这是该方法所期望的适当接口。即使它们具有相同的名称,命名空间也会有所不同。
更多相关文章
- 支付宝手机接口,服务端PHP验证失败,求助
- JSON解析错误:无法识别的标记'<'处于角度
- 在提交注册表单时使用jQuery显示错误
- 获取所有网站的错误,[重复]
- 如何让jQuery ajax执行错误功能
- Yii 访问 Gii(脚手架)时出现 403 错误
- PHP强大包括处理错误?
- php面向对象之抽象类和接口理解
- 您是否认为PHP中的错误形式是在类方法中访问超级全局变量?