没有类型声明的已实现方法的类型声明
Type declarations on implemented method which has no type declarations
我有这样的界面:
interface A {
static function from($object): self;
}
在实现接口的class中:
class B implements A {
static function from(\C $object): self{ // This is highlighted as an error
return new self();
}
}
接口没有声明类型可以不声明类型吗?
Can I not declare a type when the interface has not declared a type?
简而言之:没有。
根据 interfaces 的文档(我觉得很清楚):
The class implementing the interface must use the exact same method signatures as are defined in the interface. Not doing so will result in a fatal error.
如果您提供了更多关于您认为需要这样做的原因的详细信息,我们或许可以给出更有帮助的答案,但您只是问了一个 yes/no 问题,而答案是 - 如我说 - "no".
我有这样的界面:
interface A {
static function from($object): self;
}
在实现接口的class中:
class B implements A {
static function from(\C $object): self{ // This is highlighted as an error
return new self();
}
}
接口没有声明类型可以不声明类型吗?
Can I not declare a type when the interface has not declared a type?
简而言之:没有。
根据 interfaces 的文档(我觉得很清楚):
The class implementing the interface must use the exact same method signatures as are defined in the interface. Not doing so will result in a fatal error.
如果您提供了更多关于您认为需要这样做的原因的详细信息,我们或许可以给出更有帮助的答案,但您只是问了一个 yes/no 问题,而答案是 - 如我说 - "no".