Reputation: 1541
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
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
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
include( 'E:\myproject\module\FileServer\src\FileServer\Service\Data.php' )
which is throwing PHP fatal error. Below is the complete error message
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
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?
Upvotes: 2
Views: 148
Reputation: 21759
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.
Upvotes: 1