static return type in PHP 7 interfaces -
why not possible in php 7, declare interface static
return type?
let's have following classes:
interface bignumber { /** * @param bignumber $that * * @return static */ public function plus(bignumber $that); } class biginteger implements bignumber { ... } class bigdecimal implements bignumber { ... }
i want enforce return type of plus()
method static
, is:
biginteger::plus()
must returnbiginteger
bigdecimal::plus()
must returnbigdecimal
i can declare interface in following way:
public function plus(bignumber $that) : bignumber;
but doesn't enforce above. is:
public function plus(bignumber $that) : static;
but php 7, date, not happy it:
php parse error: syntax error, unexpected 'static' (t_static)
is there specific reason this, or bug should reported?
it's not bug, doesn't make sense design-wise object-oriented programming perspective.
if biginteger
, bigdecimal
implement both bignumber
, care contract fulfil. case, it's bignumber
's interface.
so return type should using in interface bignumber
since coding against interface not know else members of interface. if need know 1 returned, interface perhaps wide in first place.
note: programming languages generics can achieve effect specifying return type generic type, php not have generics , not have in near future.
Comments
Post a Comment