In parts 1 and 2 of this series, we looked at PHP namespace basics, the use operator, and name resolution. In this final article we discuss some of the more advanced namespace options.

The __NAMESPACE__ Constant

__NAMESPACE__ is a PHP string that always returns the current namespace name. In the global space it will be an empty string.

<?php namespace App\Lib1; echo __NAMESPACE__; // outputs: App\Lib1 ?>

The value has obvious benefits during debugging. It can also be used to dynamically generate a fully-qualified class name, e.g.

<?php namespace App\Lib1; class MyClass { public function WhoAmI() { return __METHOD__; } } $c = __NAMESPACE__ . '\\MyClass'; $m = new $c; echo $m->WhoAmI(); // outputs: App\Lib1\MyClass::WhoAmI ?>

The namespace Keyword

The namespace keyword can be used to explicitly reference an item within the current namespace or a sub-namespace. It is the namespace equivalent of self within classes:

<?php namespace App\Lib1; class MyClass { public function WhoAmI() { return __METHOD__; } } $m = new namespace\MyClass; echo $m->WhoAmI(); // outputs: App\Lib1\MyClass::WhoAmI ?>

Autoloading Namespaced Classes

One of the best time-saving features of PHP 5 is autoloading. In global (non-namespaced) PHP code, a standard autoload function could be written:

<?php $obj1 = new MyClass1(); // classes/MyClass1.php is auto-loaded $obj2 = new MyClass2(); // classes/MyClass2.php is auto-loaded // autoload function function __autoload($class_name) { require_once("classes/$class_ name.php"); } ?>

In PHP 5.3, you can create an instance of a namespaced class. In that situation, the fully-qualified namespace and class name is passed to the __autoload function, e.g. the value of $class_name could be “AppLib1MyClass”. You could continue to place all your PHP class files in the same folder and strip the namespace from the string, however, that could result in file name clashes.

Alternatively, your class file hierarchy could be organized in the same way as your namespace structure. For example, a MyClass.php file could be created in the folder /classes/App/Lib1:

\classes\App\Lib1\MyClass.php:

<?php namespace App\Lib1; class MyClass { public function WhoAmI() { return __METHOD__; } } ?>

A file in root folder could then use the following code:

myapp.php:

<?php use App\Lib1\MyClass as MC; $obj = new MC(); echo $obj->WhoAmI(); // autoload function function __autoload($class) { // convert namespace to full file path $class = 'classes\' . str_replace('\\', '/', $class) . '.php'; require_once($class); } ?>

Explanation:

The class AppLib1MyClass is aliased as MC. new MC() is translated to new AppLib1MyClass() during compilation. The string “AppLib1MyClass” is passed to the __autoload function. This replaces all namespace backslashes with file path forward slashes, and modifies the string so the file “classesAppLib1MyClass.php” is loaded.

I hope you found this series of PHP namespace articles useful. Will you be namespacing in your PHP code?

See also: