【问题标题】:Declaration of method must be compatible with that of interface method - when method signatures are exactly the same?方法声明必须与接口方法的声明兼容 - 当方法签名完全相同时?
【发布时间】:2013-10-25 14:27:01
【问题描述】:

我在我正在构建的 Laravel 4 应用程序中遇到了一个非常奇怪的问题,尽管这个问题更多地与 PHP 相关而不是 Laravel:PHP 抱怨当接口和类方法都有时这些方法不兼容完全相同的签名

它应该只在例如使用了不正确的类型提示,或者参数数量不一致时才会抱怨,但由于某种原因,当一切都正确时,它会抱怨。我看不到其他遇到此问题的人,任何人都可以看到我没有看到的任何内容吗?

界面:

<?php
namespace Repository;

interface TillRepositoryInterface {
    public static function allInVenue(Venue $venue);

    public static function findByIdInVenue(Venue $venue);
}

实现接口的仓库类:

<?php

class TillRepository extends BaseRepository implements Repository\TillRepositoryInterface {

    public static function allInVenue(Venue $venue)
    {

    }

    public static function findByIdInVenue(Venue $venue)
    {

    }
}

【问题讨论】:

  • 当您从接口和实现它的类中删除类型提示 (Venue) 时它会报错吗?
  • 它没有,但是我刚刚意识到该接口正在使用命名空间,因此我需要在 Venue 之前添加一个反斜杠以使其匹配。糟糕!

标签: php interface laravel laravel-4


【解决方案1】:

似乎在发布问题后几秒钟我的大脑就打开了:

事实上我在界面中使用了一个命名空间,所以(Venue $venue)实际上是(Repository\Venue $venue)。简单地改变这个:

public static function allInVenue(Venue $venue);

public static function findByIdInVenue(Venue $venue);

到这里

public static function allInVenue(\Venue $venue);

public static function findByIdInVenue(\Venue $venue);

解决了这个问题。保持这一点,以防其他人偶然发现同样的错误,以避免头痛

【讨论】:

  • 我想说的完全一样。我在开发包时遇到了一个看起来很像的问题。
  • 天哪,我以为我快疯了 - 即使声明是 a) 平凡和 b) 完全相同,也有同样的错误。但我错过了“使用 [namespace] 作为 [alias]”的声明。感谢您使用解决方案更新您的问题 :)!
  • 没问题!其中一件事,一旦你知道它,你就会不断遇到它,但实际上知道解决方案,以免浪费时间寻找一个 :) 几乎一年后仍然发生在我身上!
猜你喜欢
  • 1970-01-01
  • 2016-06-11
  • 1970-01-01
  • 1970-01-01
  • 2011-03-08
  • 1970-01-01
相关资源
最近更新 更多