Software development contracts are more valuable than their implementation.
Here are a few reasons:
You can test classes that depend on the interface without relying on the implementation of the interface (which in itself may be a mistake). Example with PHPUnit:
//Will return an object of this type with all the methods returning null. You can do more things with the mock builder as well $mockInterface = $this->getMockBuilder("MyInterface")->getMock(); $class = new ClassWhichRequiresInterface($mockInterface); //Test class using the mock
You can write a class that uses a contract without the need for implementation, for example.
function dependentFunction(MyInterface $interface) { $interface->contractMethod();
Have one contract, but several implementations.
interface FTPUploader { } class SFTPUploader implements FTPUploader { } class FTPSUploader implements FTPUploader { }
Laravel offers support for the latter using its service container as follows:
$app->bind(FTPUploader::class, SFTPUploader::class); resolve(FTPUploader::class);
Then there is the fact that it is easier to document interfaces, since there are no real implementations, so they are still readable.
source share