Search code examples
phpclasscontrollerzend-framework2zend-controller-plugin

calling a php class function syntax


I am currently looking a this piece of code from a module called ZfcUser for Zend 2:

namespace ZfcUser\Controller;

use Zend\Form\Form;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Stdlib\ResponseInterface as Response;
use Zend\Stdlib\Parameters;
use Zend\View\Model\ViewModel;
use ZfcUser\Service\User as UserService;
use ZfcUser\Options\UserControllerOptionsInterface;

class UserController extends AbstractActionController
{
/**
 * @var UserService
 */
protected $userService;
     .
     .

public function indexAction()
{
    if (!$this->zfcUserAuthentication()->hasIdentity()) {
        return $this->redirect()->toRoute('zfcuser/login');
    }
    return new ViewModel();
}
    .
    .
}

In the namespace ZfcUser\Controller\Plugin:

namespace ZfcUser\Controller\Plugin;

use Zend\Mvc\Controller\Plugin\AbstractPlugin;
use Zend\Authentication\AuthenticationService;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use Zend\ServiceManager\ServiceManager;
use ZfcUser\Authentication\Adapter\AdapterChain as AuthAdapter;

class ZfcUserAuthentication extends AbstractPlugin implements ServiceManagerAwareInterface
{
/**
 * @var AuthAdapter
 */
protected $authAdapter;
    .
    .
/**
 * Proxy convenience method
 *
 * @return mixed
 */
public function hasIdentity()
{
    return $this->getAuthService()->hasIdentity();
}
/**
 * Get authService.
 *
 * @return AuthenticationService
 */
public function getAuthService()
{
    if (null === $this->authService) {
        $this->authService = $this->getServiceManager()->get('zfcuser_auth_service');
    }
    return $this->authService;
}

My Questions:

  • From indexAction(), the controller plugin is called without being instantiated ($this->zfcUserAuthentication()->hasIdentity()), do controller plugins always work like this?.
  • What really happens in the hasIdentity()? I see getAuthService() returning something but not hasIdentity().I am not familiar with this type of advanced class implementation of function calling so I would truly appreciate any explanation here or topic I should look into.

Solution

  • I can't answer your first question, but regarding your second question:

    The getAuthService() method in your code returns an AuthenticationService object, which has a hasIdentity() method.

    So there are two different hasIdentity() methods:

    • In the AuthenticationService class (source code here).
    • In the ZfcUserAuthentication class which you're looking at.

    This line of code in the ZfcUserAuthentication class:

    return $this->getAuthService()->hasIdentity();
    

    does three things:

    • $this->getAuthService() returns an AuthenticationService object.
    • The hasIdentity() method of that AuthenticationService object is then called, and it returns a boolean.
    • That boolean is then returned.

    Imagine splitting the code into two parts:

    // Get AuthenticationService object     Call a method of that object
    $this->getAuthService()                 ->hasIdentity();
    

    Hope that helps!