How to use Namespaces and Sub-Namespaces with PHP 5.3

If you want to implement a huge project, it will be better when you encapsulate your modules and classes into several Namespaces and Sub-Namespaces.

But why should you use namespaces?
Namespaces are very useful, if you have some classes in your project, which have the same name as other global classes or classes from other project spaces. If you have two same named classes in the same space, you will have some problems. It is also very useful, to get your project into a modular and easy-extensible structure.

As Sample, we have a project with the following structure:

As Sample, we have a project with the following structure:


      – Module1
      – Module1
      – Module1

From these, we create a similar directory-structure, like:


Now you can structure your classes as your Namespaces and find easily your classes.

To define a Namespace in a class-file, the file-contents (as sample for the MyProject.class.php) file must start with:

namespace MyProject;

class MyProjectClass {

Attention: Namespace-Files must be started with the Namespace-Definition, otherwise you will get an Fatal error:

Namespace declaration statement has to be the very first statement in the script in

The Project-Modules, which are also included in the namespace MyProject, we will encapsulate in a sub-Namespace. Therefore you must define the Sub-Namespace in the Module-Files, like:

namespace MyProject\\Module1;

class Module1 {

Now your are able to use the Namespaces in your API-File:

namespace MyProject;
require_once '../MyProject/include.php';

try {
	$MyProjectClass = new MyProjectClass();
	$Module1Class = new Module1\\Module1Class();

catch (Exception $e) {

If we now consider the namespaces in more detail, there is a similar behavior to a file system.
What we have done?
We switched in the namespace (or as sample directory) MyProject, and could directly access our Classes from these Namespace (here as the directory-sample, the files in the directory). Would we now access a global Class (which were in our directory-sample a file in our root-directory), we just add a backslash before the global Identifer:

throw new \\Exception(...

If you want to handle a Class from a Sub-Namespace, you have to add the Namespace-Path (also similar to an Filesystem):

$Module1Class = new Module1\\Module1Class();

Notice: I’ve integrate the Namspaces into an existing project. The result were some Errors like:
Fatal error: Class ‘MyProject\\Exception’ not found in..
Because the global Exception-class isn’t anymore found, which are very often used in my sourcecode. For a quick’n’dirty solution, you can do something like that:

namespace MyProject;
class Exception extends \\Execption {

Thanks of the Namespaces, it doesn’t make a Problem to named the Class also Exception 😉

Notice: The Code-Highlighting WordPress-Plugin i use seems to have a little Bug! In the Single-View of a Post a Backslash must be escaped, but in the List-View not. So, if you find a false double-‘\\’, it is a problem with these Plugin 😉

  1. No comments yet.

  1. No trackbacks yet.