I am a self-taught programmer and I am learning Zend Framework 2 at the moment.
I always wonder why every times when I'm trying to include a certain service, they are always required me to use Interface version of it.
For example, if I'm trying to use Service Locator, I will have to include serviceLocatorInterface in order to use Service Locator. Why can't I just use Service Locator class itself.
Here is from Abstract Factory class.
use Zend\ServiceManager\ServiceLocatorInterface;
Then I would use in it this way
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
Here is another example from Zend tutorial, https://framework.zend.com/manual/2.4/en/in-depth-guide/services-and-servicemanager.html#bringing-the-service-into-the-controller
use Blog\Service\PostServiceInterface;
We include PostServiceInterface. Why not just PostService?
public function __construct(PostServiceInterface $postService)
We use the PostServiceInterface here. Why not just PostService as a Type.
I am sure this is a very simple answer that all students can answer but since i'm learning this myself so I am having a hard time understanding it.
PS. I do understand the concept of interface and inheritance. I just don't know why we include Interface this way.
Edit: After the answer I found a link that helps me understand better why people are passing interface as a type dependency instead of a concrete type.
What is the difference between an interface and abstract class?
http://kristopherwilson.com/2015/03/26/using-interfaces-effectively-in-php/
I hope those links help someone elses too.