Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP 7 interfaces, return type hinting and self

UPDATE: PHP 7.4 now does support covariance and contravariance which addresses the major issue raised in this question.


I have run into something of an issue with using return type hinting in PHP 7. My understanding is that hinting : self means that you intend for an implementing class to return itself. Therefore I used : self in my interfaces to indicate that, but when I tried to actually implement the interface I got compatibility errors.

The following is a simple demonstration of the issue I've run into:

interface iFoo {     public function bar (string $baz) : self; }  class Foo implements iFoo {      public function bar (string $baz) : self     {         echo $baz . PHP_EOL;         return $this;     } }  (new Foo ()) -> bar ("Fred")      -> bar ("Wilma")      -> bar ("Barney")      -> bar ("Betty"); 

The expected output was:

Fred Wilma Barney Betty

What I actually get is:

PHP Fatal error: Declaration of Foo::bar(int $baz): Foo must be compatible with iFoo::bar(int $baz): iFoo in test.php on line 7

The thing is Foo is an implementation of iFoo, so as far as I can tell the implementation should be perfectly compatible with the given interface. I could presumably fix this issue by changing either the interface or the implementing class (or both) to return hint the interface by name instead of using self, but my understanding is that semantically self means "return the instance of the class you just called the method on". Therefore changing it to the interface would mean in theory that I could return any instance of something that implements the interface when my intent is for the invoked instance is what will be returned.

Is this an oversight in PHP or is this a deliberate design decision? If it's the former is there any chance of seeing it fixed in PHP 7.1? If not then what is the correct way of return hinting that your interface expects you to return the instance you just called the method on for chaining?

like image 553
GordonM Avatar asked Aug 21 '16 21:08

GordonM


People also ask

What is type hinting in PHP?

Type hinting is a concept that provides hints to function for the expected data type of arguments. For example, If we want to add an integer while writing the add function, we had mentioned the data type (integer in this case) of the parameter.

How to specify return type in PHP?

There is no way to define the return type of a function within PHP as it is loosely-typed. You will have to do your validation within the calling function.

When was type hinting added PHP?

In May of 2010 support for scalar type hinting was added to the PHP trunk.

What is return void in PHP?

A void return type has been introduced. Functions declared with void as their return type must either omit their return statement altogether, or use an empty return statement. null is not a valid return value for a void function.


2 Answers

editorial note: the answer below is outdated. as php PHP7.4.0, the following is perfectly legal:

<?php Interface I{     public static function init(?string $url): self; } class C implements I{     public static function init(?string $url): self{         return new self();     } } $o = C::init("foo"); var_dump($o); 
  • 3v4l: https://3v4l.org/VYbGn

original answer:

self does not refer to the instance, it refers to the current class. There is no way for an interface to specify that the same instance must be returned - using self in the manner you're attempting would only enforce that the returned instance be of the same class.

That said, return type declarations in PHP must be invariant while what you're attempting is covariant.

Your use of self is equivalent to:

interface iFoo {     public function bar (string $baz) : iFoo; }  class Foo implements iFoo {      public function bar (string $baz) : Foo  {...} } 

which is not allowed.


The Return Type Declarations RFC has this to say:

The enforcement of the declared return type during inheritance is invariant; this means that when a sub-type overrides a parent method then the return type of the child must exactly match the parent and may not be omitted. If the parent does not declare a return type then the child is allowed to declare one.

...

This RFC originally proposed covariant return types but was changed to invariant because of a few issues. It is possible to add covariant return types at some point in the future.


For the time being at least the best you can do is:

interface iFoo {     public function bar (string $baz) : iFoo; }  class Foo implements iFoo {      public function bar (string $baz) : iFoo  {...} } 
like image 131
user3942918 Avatar answered Oct 09 '22 08:10

user3942918


It also can be a solution, that you don't define explicitly the return type in the Interface, only in the PHPDoc and then you can define the certain return type in the implementations:

interface iFoo {     public function bar (string $baz); }  class Foo implements iFoo {     public function bar (string $baz) : Foo  {...} } 
like image 36
Gabor Avatar answered Oct 09 '22 07:10

Gabor