class 继承的 PHPStorm 9 检查工作出乎意料
PHPStorm 9 inspection of class inheritance works unexpectable
我在 PHPStorm 9 中遇到以下问题:
假设我有一个接口 FieldInterface
,它有一些方法:
namespace Acme;
interface FieldInterface {
public function methodA();
public function methodB();
}
然后我有一个实现接口基本功能的抽象 class。那个抽象 class 让用户实现某些方法,在我们的例子中假设它是 methodB
:
namespace Acme;
abstract class AbstractField implements FieldInterface {
public function methodA() {
// implement methodA
}
public abstract function methodB(); // have the user implement it
}
最后我有一些现成的 class StringField
:
namespace Acme;
class StringField extends AbstractField {
public function methodB() {
// implement methodB
}
}
此时一切顺利。但是如果我在 FieldInterface
中添加新方法,PHPStorm 不会说 AbstractField
有任何问题,而很明显我应该在其中添加 public abstract function newMethod();
。但是,它会在 StringField
class 中发现错误。
从抽象classes是为了扩展的目的可以理解,但通常你扩展抽象class而不是实现底层接口。做abstract class 的全部意义就是节省用户实现接口的时间。那么为什么 PHPStorm 强迫我在 具体 class 中实现接口而不是强迫我在 抽象 class 中实现它明确实现了接口。
所以我想知道它是否是 PHPStorm 中的错误,或者它可能是故意的。无论哪种方式,是否有任何解决方法?
应该是这样,在摘要中显示错误class是错误的。
事实上,public abstract function methodB();
是多余的,因为抽象 class 已经 "inherits" 来自接口的抽象方法,因为它没有实现它。
唯一的解决方法是使AbstractField
不是抽象。
我在 PHPStorm 9 中遇到以下问题:
假设我有一个接口 FieldInterface
,它有一些方法:
namespace Acme;
interface FieldInterface {
public function methodA();
public function methodB();
}
然后我有一个实现接口基本功能的抽象 class。那个抽象 class 让用户实现某些方法,在我们的例子中假设它是 methodB
:
namespace Acme;
abstract class AbstractField implements FieldInterface {
public function methodA() {
// implement methodA
}
public abstract function methodB(); // have the user implement it
}
最后我有一些现成的 class StringField
:
namespace Acme;
class StringField extends AbstractField {
public function methodB() {
// implement methodB
}
}
此时一切顺利。但是如果我在 FieldInterface
中添加新方法,PHPStorm 不会说 AbstractField
有任何问题,而很明显我应该在其中添加 public abstract function newMethod();
。但是,它会在 StringField
class 中发现错误。
从抽象classes是为了扩展的目的可以理解,但通常你扩展抽象class而不是实现底层接口。做abstract class 的全部意义就是节省用户实现接口的时间。那么为什么 PHPStorm 强迫我在 具体 class 中实现接口而不是强迫我在 抽象 class 中实现它明确实现了接口。
所以我想知道它是否是 PHPStorm 中的错误,或者它可能是故意的。无论哪种方式,是否有任何解决方法?
应该是这样,在摘要中显示错误class是错误的。
事实上,public abstract function methodB();
是多余的,因为抽象 class 已经 "inherits" 来自接口的抽象方法,因为它没有实现它。
唯一的解决方法是使AbstractField
不是抽象。