Narrowing-down the return type of an implemented method

前端 未结 2 395
-上瘾入骨i
-上瘾入骨i 2021-01-23 05:06

I\'m trying to specify two interfaces for class A that returns instances of class B, and for class B itself.

I\'m declaring return types on the interface.

Sa

2条回答
  •  我在风中等你
    2021-01-23 05:48

    Happy to answer my own question after a year or so:

    This will be possible in PHP 7.4

    This will be possible:

    interface X {
      function m(Y $z): X;
    }
    interface Y extends X {
      // not permitted but type-safe
      function m(X $z): Y;
    }
    

    Check out this RFC.

提交回复
热议问题