Yet another blog a place for my scribbles

Applying Decorator Pattern In ZendFramework

A big refactoring job at my workplace has led me to review some of the ViewHelpers we are having in one of our core applications. These helpers where targetting different types of properties resulting in the use of several view helpers for a single view and the same view helper being used by multiple (unrelated) views. This results in a high coupling whereas we would like a lower coupling.

In my opinion we have improved by implementing a series of Decorator Pattern for the individual entities.


namespace App\View\Helper;

use Zend\View\Helper\AbstractHelper;

class UserDecorator extends AbstractHelper
    /** @var User */
    private $user;

     * Clone the user decorator with the provided user
     * @param User $user
     * @return UserDecorator
    public function __invoke(User $user)
        $clone = clone $this;
        $clone->user = $user;

        return $clone;
     * Get the full name of the user
     * @return string
    public function getFullName()
        return sprintf(
            '%s %s',

Some plumping needs to be done in order to get the decorator available in the view:

# ./module/App/config/module.config.php

use App\View\Helper;

return [
  'view_helpers' => [
    'invokables' => [
      'userDecorator' => Helper\UserDecorator::class

Using it in the view is rather simple:

$user = $this->userDecorator($this->user);

printf('Full name: %s', $user->getFullName());