dougong1031 2016-09-16 19:22
浏览 14
已采纳

在没有类型声明的已实现方法上键入声明

I have an interface like so:

interface A {
    static function from($object): self;
}

In the class that implements the interface:

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?

  • 写回答

1条回答 默认 最新

  • down_load1117 2016-09-16 22:10
    关注

    Can I not declare a type when the interface has not declared a type?

    In short: no.

    As per the docs for interfaces (which seem pretty clear to me):

    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.

    If you provided more detail as to why you think you need to do this, we could perhaps give a more helpful answer, but yer just asking a yes/no question, and the answer to that is - as I said - "no".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?