Search code examples
phpinterfacenamespacesphp-5.3

Why does this PHP method declaration not recognize the namespace of the type hint?


I'm trying to write a library based on Doctrine Extensions, which provides this interface:

namespace Gedmo\Mapping;

use Doctrine\Common\Persistence\Mapping\ClassMetadata;

interface Driver {
  public function readExtendedMetadata(ClassMetadata $meta, array &$config);

  public function setOriginalDriver($driver);
}

In my own code I implement this interface:

namespace Avit\Schedulable\Mapping\Driver;

use Gedmo\Mapping\Driver,
    Doctrine\Common\Persistence\Mapping\ClassMetadata,
    Doctrine\Common\Annotations\AnnotationReader;

class Annotation implements Driver {
  public function readExtendedMetadata(ClassMetadata $meta, array &$config) {
    // my implementation
  }
}

The error I get says: Declaration of Avit\Schedulable\Mapping\Driver\Annotation::readExtendedMetadata() must be compatible with that of Gedmo\Mapping\Driver::readExtendedMetadata()

I can avoid the error if I remove the ClassMetadata type hint on my method.

I've googled some information that says this error happens when the namespace is mismatched. This makes sense since I'm declaring this class in my own namespace, however, I'm aliasing the same Doctrine namespace for ClassMetadata that the original Driver interface uses.

Why is the type hint not recognized?


Solution

  • The method in Driver here https://github.com/l3pp4rd/DoctrineExtensions/blob/master/lib/Gedmo/Mapping/Driver.php seems do not having ClassMetadata type hint.

    Mind if check the one you are using?