Namespaces in PHP

For a while I have not been using namespaces in PHP but the time has come.

In this article I mostly explain namespaces to myself but if you find this useful that’s good as well.

Working with several projects lately I noticed that third-party libraries are often used and you may have a situation when you want give a class or a function the same name used in other library. This, and ability of shortening class names to avoid names like Zend_Translate_Adapter_Array, namespaces exist for.

Not sure about you but I only consume information from good examples, so here they are.

So, first we define namespaces, let’s say that is lib.php file containing a library:

<?php
namespace Lib;
class Bootstrap
{
    public function init()
    {
        print "init" . PHP_EOL;
    }
}

Then we use them, this is app.php file containing the application:

<?php
 
require_once 'lib.php';
 
$app = new \Lib\Bootstrap;
$app->init();

Or if we plan to use function names without qualifying/full prefixed name like Lib\Bootstrap we could just import that:

<?php
 
require_once 'lib.php';
 
use \Lib\Bootstrap;
$app = new Bootstrap;
$app->init();

But the real power of use is in aliasing:

<?php
 
require_once 'lib.php';
 
use \Lib\Bootstrap as StandardBootstrap;
$app = new StandardBootstrap;
$app->init();

Which allows us to be very flexible about class and namespaces names.

If we run the code above we get:

alex@desktop:namespaces$ php app.php 
init

An example from Zend Framework 2, notice importing other namespaces and using some aliases:

<?php
namespace Zend\View;
 
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerAwareInterface;
use Zend\Mvc\MvcEvent;
use Zend\Stdlib\RequestInterface as Request;
use Zend\Stdlib\ResponseInterface as Response;
...
class View implements EventManagerAwareInterface
{
...
    public function setRequest(Request $request)
    {
        $this->request = $request;
        return $this;
    }

Some other use was mentioned in comments on php.net where it was said that namespaces may be used to version the API, e.g.

namespace CoolAPI\v1 
{
// namespace v1 code
}
namespace CoolAPI\v2;
{
// namespace v2 code
}

So namespaces are used to separate or rather group classes in much better way and be a bit more flexible when using them.

If you want to know more the best place to go is PHP.net site and here is the link to Namespaces section.

This entry was posted in Uncategorized and tagged . Bookmark the permalink.

One Response to Namespaces in PHP

  1. Mike Pearce says:

    Nice post Alex.

    My test to join Affiliate Window consisted of me arsing about with namespaces and using them to autoload and dependency inject, check it out: https://github.com/MikePearce/Data-Reader/blob/master/scripts/report.php

Leave a Reply

Your email address will not be published. Required fields are marked *

*

CAPTCHA image
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">