Why is it possible to create an interface without specifying a return type? Why doesn’t this make this interface unusable?
This makes it more clear:
Interface run
{
public function getInteger();
}
class MyString implements run
{
public function myNumber()
{
}
public function getInteger()
{
return "Not a number";
}
}
In Java every Interface has a return type like Integer, String or Void
I know that PHP is unfortunately a loosely typed language but isn’t there a solution to that problem?
Is it possible to define an Interface with a return type like Integer?
Type hinting for function/method arguments or return values is only supported at PHP7.0 or later
Check the details here:
http://php.net/manual/en/migration70.new-features.php
If you are using PHP5 so the current accepted practice is to use phpdoc comments to indicate the “contract” is present.
If the code violates the “contract,” I suggest finding the original coder and having them fix it and/or filing a bug and/or fixing it yourself if it’s in your own codebase.