Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static return type in PHP 7 interfaces

Tags:

Why is it not possible in PHP 7, to declare an interface with a static return type?

Let's say I have the following classes:

interface BigNumber {
    /**
     * @param BigNumber $that
     *
     * @return static
     */
    public function plus(BigNumber $that);
}

class BigInteger implements BigNumber { ... }
class BigDecimal implements BigNumber { ... }

I want to enforce the return type of the plus() method to static, that is:

  • BigInteger::plus() must return a BigInteger
  • BigDecimal::plus() must return a BigDecimal

I can declare the interface in the following way:

public function plus(BigNumber $that) : BigNumber;

But that doesn't enforce the above. What I would like to do is:

public function plus(BigNumber $that) : static;

But PHP 7, to date, is not happy with it:

PHP Parse error: syntax error, unexpected 'static' (T_STATIC)

Is there a specific reason for this, or is this a bug that should be reported?

like image 581
BenMorel Avatar asked Jun 20 '15 10:06

BenMorel


People also ask

What is the return type of static in PHP?

PHP 8.0 allows static as a return type for class methods. PHP class methods can return self and parent in previous versions, but static was not allowed in PHP versions prior to 8.0. The newly allowed static return type allows to narrow down the return type to the called class.

What is return type in PHP 7?

In PHP 7, a new feature, Return type declarations has been introduced. Return type declaration specifies the type of value that a function should return. Following types for return types can be declared. int. float. bool. string. interfaces.

What is the return type of getInstance () in PHP?

PHP 8.0 allows static as a return type for class methods. class Foo { public static function getInstance(): static { return new static(); } } PHP class methods can return self and parent in previous versions, but static was not allowed in PHP versions prior to 8.0.

What is the use of static return in Java?

The newly allowed static return type allows to narrow down the return type to the called class. The static return type helps classes with fluent methods (i.e the ones with return $this ), immutable classes (i.e return clone $this) or static methods that return an instance of the class itself.


2 Answers

2020 update

Static return types have been introduced in PHP 8.

like image 199
BenMorel Avatar answered Sep 17 '22 19:09

BenMorel


It's not a bug, it just doesn't make sense design-wise from an object-oriented programming perspective.

If your BigInteger and BigDecimal implement both BigNumber, you care about the contract they fulfil. I this case, it's BigNumber's interface.

So the return type you should be using in your interface is BigNumber since anybody coding against that interface does not know anything else than members of that interface. If you need to know about which one is returned, the interface is perhaps too wide in the first place.

Note: programming languages with generics can achieve this effect by specifying the return type as the generic type, but PHP does not have generics and probably will not have in the near future.

like image 23
vvondra Avatar answered Sep 20 '22 19:09

vvondra