用派生类型替换抽象方法的参数类型

时间:2014-02-09 22:14:09

标签: php parameter-passing abstract derived-class

这就是我所拥有的:

<?php
abstract class BasePlayer {
  ...
}

class Player extends BasePlayer {
  ...
}

abstract class BaseRoom {
  abstract public function joinPlayer(BasePlayer $player);
}

class Room extends BaseRoom {
  public function joinPlayer(Player $player) {
    ...
  }
}

长话短说,在声明中我使用了一个抽象(通用)类,而在实现中我使用了这个抽象的子类。不幸的是,它抛出

PHP Fatal error:  Declaration of Room::joinPlayer() must be compatible with BaseRoom::joinPlayer(BasePlayer $player)

如果您对此问题提出任何建议,我将非常感激。

0 个答案:

没有答案