This code is ZF2, but this feels like it might be a more general OOP question because its about some confusion I have with interfaces. This example is a little long winded, but I wanted to use all of my actually code to show everything that I'm doing.
So here is the code snippet that is the source of my confusion:
// References
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\DbTable as DbTableAuthAdapter;
// Class definition
public function getAuthService()
{
if (! $this->authservice) {
$dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
$dbTableAuthAdapter = new DbTableAuthAdapter($dbAdapter, 'user','email','password', 'MD5(?)');
$authService = new AuthenticationService();
$authService->setAdapter($dbTableAuthAdapter);
$this->authservice = $authService;
}
return $this->authservice;
}
public function processAction()
//
$this->getAuthService()->getAdapter()
->setIdentity($this->request->getPost('email'))
->setCredential($this->request->getPost('password'));
$result = $this->getAuthService()->authenticate();
if ($result->isValid()) {
$this->getAuthService()->getStorage()->write($this->request->getPost('email'));
return $this->redirect()->toRoute(NULL , array(
'controller' => 'login',
'action' => 'confirm'
));
}
In the first method we construct a new Zend\Authentication\Adapter\DbTable with some params, pass it to an instance of Zend\Authentication\AuthenticationService and then return that instance of AuthenticationService.
In the next method we call that method ($this->getAuthService()) to get an instance of AuthenticationService, call the AuthenticationService's getAdapter() method and then start calling Zend\Authentication\Adapter\DbTable method on the object returned.
Here is what confuses me. Look at the definition getAdater(). It doesn't actually return an instance of Zend\Authentication\Adapter\DbTable it only returns an interface: Zend\Authentication\Adapter\AdapterInterface and this interface doesn't define any Zend\Authentication\Adapter\DbTable methods.
So if getAdapter() is only returning an interface how is it I am able to call Zend\Authentication\Adapter\DbTable methods on the object returned?
Sorry if this question is confusing to read I'm confused about what's happening here on a pretty fundamental level so its difficult for me to be more lucid.