【问题标题】:Why does this PHP method declaration not recognize the namespace of the type hint?为什么这个 PHP 方法声明不能识别类型提示的命名空间?
【发布时间】:2012-02-13 14:45:00
【问题描述】:

我正在尝试写一个基于Doctrine Extensions的库,它提供了这个接口:

namespace Gedmo\Mapping;

use Doctrine\Common\Persistence\Mapping\ClassMetadata;

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

  public function setOriginalDriver($driver);
}

在我自己的代码中,我实现了这个接口:

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
  }
}

我得到的错误是:Avit\Schedulable\Mapping\Driver\Annotation::readExtendedMetadata() 的声明必须与 Gedmo\Mapping\Driver::readExtendedMetadata() 的声明兼容

如果我删除方法上的ClassMetadata 类型提示,我可以避免该错误。

我在谷歌上搜索了一些信息,这些信息说当命名空间不匹配时会发生此错误。这是有道理的,因为我在我自己的命名空间中声明了这个类,但是,我为 ClassMetadata 使用了与原始 Driver 接口使用的相同的 Doctrine 命名空间。

为什么无法识别类型提示?

【问题讨论】:

  • 第二个代码中使用后缺少的分号是错字吗?
  • 您的命名空间中是否有自己的ClassMetadata-class 或-interface?
  • 修正了错字。 @KingCrunch,不,我在任何地方都没有自己对该类的定义。即使我这样做了,“use”子句中命名的那一项不会优先吗?
  • @AndrewVit 不,它不会,但你应该得到警告,或者通知。

标签: php interface namespaces php-5.3


【解决方案1】:

Driver中的方法在这里 https://github.com/l3pp4rd/DoctrineExtensions/blob/master/lib/Gedmo/Mapping/Driver.php 似乎没有ClassMetadata 类型提示。

介意检查一下你正在使用的那个吗?

【讨论】:

  • 谢谢,看起来这是这个库中的recent change。不过,我很困惑为什么它不会将其视为相同的方法签名。
猜你喜欢
  • 2011-06-03
  • 1970-01-01
  • 2015-11-21
  • 1970-01-01
  • 2012-11-09
  • 2016-02-23
  • 2011-12-10
  • 2020-01-14
相关资源
最近更新 更多