PHP Namespace Tutorial

Oct 16
2010

Introduction

Namespaces have been around for years in other languages, but only as of PHP 5.3 have PHP developers been so fortunate. So, what is a namespace in PHP? In short, it’s an abstract container that allows us to re-use same function, class, and constant names but apply different meanings based on what context they are in. That’s a mouthful, huh?

Lets take a look at a small code snippet, and then we’ll break down and expand upon what’s going on in small, chewable increments:

namespace_intro.php

namespace Foo;

function Bar()
{
    echo __NAMESPACE__;
}

namespace FooFoo;

function Bar()
{
    echo "Bar";
}

echo \Foo\Bar();

Output:

Foo

There are a few points of interest here:

  • The namespace keyword
  • The __NAMESPACE__ constant
  • Redefinition of the Bar() function
  • The strange new syntax in the echo statement at the end: name resolution

The namespace Keyword

Namespaces are named arbitrarily and have two syntax forms. Note, in either syntax, namespaces cannot be nested; although, sub-namespaces can be defined ( explained later ).

Syntax 1:
namespace Foo

//...code here...
Syntax 2 – Block form:
namespace Foo
{
    //...code here...
}
Secondary Usage

The namespace keyword also has a secondary usage, which is to act as an explicit reference to the current namespace, similar to the self:: keyword used in classes. Example:

namespace_self_reference.php

namespace Foo;

class Foo
{
    public function __construct()
    {
        echo __NAMESPACE__;
    }
}

namespace Bar;

class Foo
{
    public function __construct()
    {
        echo __NAMESPACE__;
    }
}

$object = new namespace\Foo();

Output:

Bar

The __NAMESPACE__ Constant

The __NAMESPACE__ magic constant contains a string that outputs the current namespace. When called from the global context, an empty string is output. You may find it useful for dynamically constructing variable and class names, to error reporting.

Name Resolution

Name resolution refers to how namespaces find your classes, functions, and constants based on a few definitions and rules. Depending on the syntax you use, import rules, and existence in the current namespace, name resolution can change the behavior of your code – so be familiar with the rules.

Some definitions:

Unqualified Names

Unqualified names have no reference to a namespace. Example:

    Foo()

At run time, if within a namespace and a call to an unqualified function occurs, the namespace is first checked, if the function is not found, the global scope is then checked for that function.

For class names, the class is looked for in the current namespace, autoload is then called on that class.

Qualified Names

Qualified names have a namespace separator included, and are translated during compile time based on the current import rules, Example:

    Foo\Bar()

Fully Qualified Names

Fully qualified names (FQN) start with a namespace separator, and are explicit references to a namespace that cannot be effected by import rules. FQNs are resolved at compile time. Example:

    \Foo()
    \Foo\Bar()

Sub Namespaces

[coming soon...]

Namespace Aliases

[coming soon...]

Importing Namespaces

[coming soon...]

Autoloading Namespaced Classes

[coming soon...]

Conclusion

Without Namespaces

Previous to namespaces in PHP, the common alternative to control context was through careful conventions in file system structure + long class names, ie:

Framework/DB.php

class Framework_DB
{
    //etc...
}

Framework/DB/MySQL.php

class Framework_DB_MySQL extends Framework_DB
{
    //etc...
}

Application/DB.php

class Application_DB
{
    //etc...
}

Rewritten Using Namespaces

While the filesystem structure convention is still a recommended practice for organization, the contents of the files can be defined more concisely using namespaces:

Framework/DB.php

namespace Framework\DB;

class DB
{
    //etc
}

Framework/DB/Mysql.php

namespace Framework\DB;

class MySQL extends DB
{
    //etc
}

Application/DB.php

namespace Application;

class DB
{
    //etc
}

I’m anxious to see how the introduction of namespaces in PHP 5.3 changes software everywhere! Happy Coding!

Visit Other Sites!

Find me on other sites...

Archives

All entries, chronologically...

Pages List

General info about this site...