PHP实现接口方法时出现致命错误

时间:2015-09-23 17:18:29

标签: php oop zend-framework

对于zend人员的注意事项:我希望这不会成为特定于ZF的问题,因为我可以看到这与PHP(或通常是OOP)纯粹相关,但添加了ZF标签只是为了看看是否还有其他我缺少的东西

我在ZF 2中创建了一个项目,我有一个实现了一个名为的接口的类 服务感知类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();
}

我的实现者类就是这个(为了调试,我已经删除了这个类中的所有其他内容,现在这是我班级的全长代码)

<?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致命错误。以下是完整的错误消息

  

致命错误:声明   FileServer \ Service \ Data :: setServiceLocator()必须兼容   Zend的\的ServiceManager \ ServiceLocatorAwareInterface :: setServiceLocator(Zend的\的ServiceManager \ ServiceLocatorInterface   $ serviceLocator)in   E:\ myproject \ module \ FileServer \ src \ FileServer \ Service \ Data.php on   第23行

正如您所看到的,我正在使用适当的签名实现getter和setter函数,但仍然会收到错误。我错过了什么/忽略了什么?

1 个答案:

答案 0 :(得分:1)

替换:

use Zend\Di\ServiceLocatorInterface;

使用:

use Zend\ServiceManager\ServiceLocatorInterface;

因为这是该方法所期望的适当接口。即使名称相同,名称空间也不同。

相关问题