Using a PHP Namespace

Programming today encourages you to use libraries of code from other developers for added functionality. What happens when two library developers, unknowingly, name their classes, functions, or constants the same name, and then you, wanting to use that functionality, add their code into your application. Then there's your code that also may have the same name conflicts. We potentially have an environment that could lead to name collisions, which could be difficult to discover?

In PHP 5.3, "namespaces" were introduced to solve this problem. Namespaces group related code together, like a separate directory, only in code, not as an operating system directory. By giving sections of code, a namespace, you separate that code from all other code, and by doing this, separate that code from possible name collisions.

How do you create a namespace? First, you have to be running PHP 5.3 or higher. Namespaces only apply to three types of coding constructs: classes, functions, and constants. To declare a namespace, you declare it like this:

    namespace MyNameSpace;

    const EGOR = 66;
    class ChurchMonster { /* ... */ }
    function gargoyle()
    {
      echo "..."  ;
    }

Namespaces are declared before any other constructs, including white space, after the files initial php tag, like session_start. You can use the same namespace, "MyNameSpace," for example, in more than one file and in more than one directory, organizing your code across your entire application and directories into namespaces.

You can have more than one namespace in the same file, using curly braces to enclose namespace code, just so the first one starts right after the initial php tag, like so:

    namespace MySecondSpace 
    {
        define("INTRO","Hello!");
        class ChurchMonster { /* ... */  }
        function gargoyle( ) { /* ... */ } 
    }

    namespace MyThirdSpace 
    {
        define("INTRO", "Hello, Egor");
        class ChurchMonster { /* ... */  }
        function gargoyle( ) { /* ... */ } 
    }

You can have sub-namespaces, however you can not nest one namespace within another namespace. Sub-namespaces are accomplished with the use of a backslash between the names.

// like this
	namespace MySecondSpace\webapp1 { ... }
	namespace MySecondSpace\Freddie\webapp2 { ... }

// not nested - this will not work
	namespace MyThirdSpace\webapp1 { ... }
	{ 
	  namespace MythirdSpace\Freddie\webapp2 { ... }
	}

Alright, we've declared them, how do we use them?

If we want to use the same namespace in another file, we can use the "USE" keyword and specify the namespace at the top of the file, like so:

	use  MyNameSpace;

Any constant, classes, or functions in the file will now be part of "MyNameSpace".

If we want to call a constant, function, or make a new class instance in another file from different namespace, we would type:

// With a constant
echo "MyNameSpace\EGOR"; 
// With a class
$MyNewObj = new MyThirdSpace\Freddie();
// With a function
MySecondSpace\gargoyle();

How about naming namespaces? If your doing an application for your organization, you can use that as a high level namespace. Sub-namespaces could be individual projects, or domains.

The other thing namespaces can do is shorten the overall path to make your code more readable. You do this by using an alias, like so:

	// The namespace
	namespace Corporation\Division\Department\Group\Project\Merlin;
	// Using the namespace
	$WebApp = new Corporation\Division\Department\Group\Project\Merlin();

	// To make code more readable, let's use an alias
	use Corporation\Division\Department\Group\Project\Merlin as Camelot;

	// To use the alias
$WebApp = new Camelot\Merlin;

As you can see, using the alias with a long namespace makes your code much more readable.

PHP sets a constant value, __NAMESPACE__ , of the current namespace that you are working with that you can use through out your application. So if your current namespace is "CoolApp."

	echo "The currnet namespace is: " .  __NAMESPACE__ ";

	// The output would be: 
	// The current namespase is: CoolApp

You probably don't want to use a lot of namespaces to keep track of throughout your applications. You probably don't need namespaces for small applications or applications with only one developer, or applications without frameworks of third party code. If your application has more than one developer, or you start to use third-party libraries, you might want to consider using namespaces to keep your code clean, and prevent name conflicts in your code, that are difficult to find and fix.

Comments are closed.