A free and open-source book on Zend Framework for beginners


3.2. PHP Namespaces

When you use classes from different libraries (or even classes from different components of a single library) in your program, the class names may conflict. This means you can encounter two classes having the same name, resulting in a PHP interpreter error. If you've ever programmed websites with Zend Framework 1, you might remember those extra long class names like Zend_Controller_Abstract. The idea with long names was utilized to avoid name collisions between different components. Each component defined its own name prefix, like Zend_ or My_.

To achieve the same goal, Zend Framework 3 uses a PHP language feature called namespaces. The namespaces allow to solve name collisions between code components, and provide you with the ability to make the long names shorter.

A namespace is a container for a group of names. You can nest namespaces into each other. If a class does not define a namespace, it lives inside of the global namespace (for example, PHP classes Exception and DateTime belong to global namespace).

A real-world example of a namespace definition (taken from Zend\Mvc component) is presented below:

<?php
namespace Zend\Mvc;

/**
 * Main application class for invoking applications.
 */
class Application 
{
    // ... class members were omitted for simplicity ...
}

In Zend Framework 3, all classes belong to top-level Zend namespace. The line 2 defines the namespace Mvc, which is nested into Zend namespace, and all classes of this component (including the Application class shown in this example on lines 7-10) belong to this namespace. You separate nested namespace names with the back-slash character ('\').

In other parts of code, you reference the Application class using its fully-qualified name:

<?php
$application = new \Zend\Mvc\Application();

Please note the leading back-slash in \Zend\Mvc\Application name. If you specify a class name with leading back-slash, this means the fully-qualified class name. It is also possible to specify class name relatively to the current namespace, in that case you do not specify the leading back-slash.

It is also possible to use the alias (short name for the class) with the help of PHP's use statement:

<?php
// Define the alias in the beginning of the file.
use Zend\Mvc\Application;

// Later in your code, use the short class name.
$application = new Application();

Although the alias allows to use a short class name instead of the full name, its usage is optional. You are not required to always use aliases, and can refer the class by its fully-qualified name.

Every PHP file of your application typically defines the namespace (except index.php entry script and config files, which typically do not). For example, the main module of your site, the Application module, defines its own namespace whose name equals to the module name:

<?php
namespace Application;

class Module 
{
    // ... class members were omitted for simplicity ...
}

Top