§ Classes VS Namespaces

11 Feb 2016 | Anton Kril Tweet

Many PHP frameworks use classes as namespances for functions. The most widespread example is Controller classes:

<?php

class UserController
{
  public function viewAction()
  {
    // Show user
  }
  
  public function removeAction()
  {
    // Remove user
  }
}

Acthion methods in such class are not related:

If there is need to group such functions, namespaces should be used:

<?php
namespace User;

function view()
{
  // View user
}

function remove()
{
  // Remove user
}

If you need to represent operations, but, for some reason, only objects are allowed, classes with one method (commands) can be used:

<?php 
namespace User;

class View
{
  public function __invoke() // or execute()
  {
  }
}

class Remove
{
  public function __invoke()
  {
  }
}