I was going through the Laravel's Illuminate and I noticed it has an interface for nearly every implementation.
What's the exact purpose of this? Does it have any current use, or is it more to make the framework as scaleable as possible?
In software engineering contracts are more valuable than their implementations.
Here's a few reasons:
You can test classes which depend on an interface without relying on an interface implementation (which itself may be buggy). 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 which uses a contract without needing an implementation e.g.
function dependentFunction(MyInterface $interface) {
$interface->contractMethod(); // Assume it's there even though it's not yet implemented.
}
Have a single contract but multiple implementations.
interface FTPUploader { /* Contract */ }
class SFTPUploader implements FTPUploader { /* Method implementation */ }
class FTPSUploader implements FTPUploader { /* Method implementation */ }
Laravel offers support of the last one using its service container as follows:
$app->bind(FTPUploader::class, SFTPUploader::class);
resolve(FTPUploader::class); //Gets an SFTPUploader object
Then there's also the fact that its easier to document interfaces since there's no actual implementations in there so they're still readable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With