Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Return value of class::functionName() must be an instance of /Interface, null returned

How to specify a return type of function (an instance of Interface) using php prophecy.

method is like:-

class classname()
{
  public $var;

public functionName : Interface
{

   return $this->var;

}

}

IS it possible to do something like this? $this->method->willreturn(string)->ofTypeInterface

like image 801
AmDGrtst Avatar asked Nov 15 '25 20:11

AmDGrtst


1 Answers

Php return types do not allow a null value by default, so if you specify that the function should return an Interface it can only return an interface object.

So you should either return an Interface object or if you also want to allow null values you should to the following:

public functionName : ?Interface
{

}

This will accept either an Interface object or null to be returned

like image 115
Sven Avatar answered Nov 17 '25 12:11

Sven



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!