PHP 实现接口方法时出现致命错误
PHP Fatal error on implementing interface methods
zend 人员请注意:我希望这不会成为 ZF 特定的问题,因为我可以看到这纯粹与 PHP 有关(或者通常是OOP) 但添加了 ZF 标签只是为了看看是否还有其他我遗漏的东西
我正在 ZF 2 中创建一个项目,其中有一个 class 正在实现一个名为
服务意识 class Zend\ServiceManager\ServiceLocatorAwareInterface
这里的界面很简单
<?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();
}
我的实现者 class 是这样的(为了调试,我已经删除了这个 class 中的所有其他内容,现在这是我的 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;
}
}
我在 index.php
中有一个 include 语句
include( 'E:\myproject\module\FileServer\src\FileServer\Service\Data.php' )
抛出 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
如您所见,我正在使用正确的签名实现 getter 和 setter 函数,但仍然出现错误。我在missing/overlooking这里是什么?
替换:
use Zend\Di\ServiceLocatorInterface;
与:
use Zend\ServiceManager\ServiceLocatorInterface;
因为这就是该方法所期望的正确接口。尽管它们具有相同的名称,但命名空间不同。
zend 人员请注意:我希望这不会成为 ZF 特定的问题,因为我可以看到这纯粹与 PHP 有关(或者通常是OOP) 但添加了 ZF 标签只是为了看看是否还有其他我遗漏的东西
我正在 ZF 2 中创建一个项目,其中有一个 class 正在实现一个名为
服务意识 class Zend\ServiceManager\ServiceLocatorAwareInterface
这里的界面很简单
<?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();
}
我的实现者 class 是这样的(为了调试,我已经删除了这个 class 中的所有其他内容,现在这是我的 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;
}
}
我在 index.php
中有一个 include 语句include( 'E:\myproject\module\FileServer\src\FileServer\Service\Data.php' )
抛出 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
如您所见,我正在使用正确的签名实现 getter 和 setter 函数,但仍然出现错误。我在missing/overlooking这里是什么?
替换:
use Zend\Di\ServiceLocatorInterface;
与:
use Zend\ServiceManager\ServiceLocatorInterface;
因为这就是该方法所期望的正确接口。尽管它们具有相同的名称,但命名空间不同。