Namespaces

Posted on March 10th, 2009 by Sam

Namespace has been used in programming languages for quite a few years. Java uses them extensively, and many people have complained about the lack of namespace in PHP. Namespace was initially slated for PHP 5.0, but due to difficulties with its implementation, integration into the code base was postponed to version 5.3. With the release of PHP 5.3 by the end of Q1-2009, you will have the first taste of namespace in PHP. The development team has worked hard on implementing namespace into the language constructs; without breaking any compatibilities or compromising future improvements they may make.

PHP opens doors of possibilities for application development by providing namespace and all its added benefits. Essentially a namespace is equivalent to a container, by creating groups for code to belong to while running. However, these containers have the ability to connect to each other; allowing them to interact as normal code, but not have to all the time. Namespace has been added to alleviate the issue of having multiple classes with the same name, which happens more often then you may realize with collaborative or just large projects. Imagine three different towns, each town with there own set of streets (each with there own set of names.) These towns may even have streets that are named the same. That could be confusing if those two streets were in the same town, but because they exist in there own town (container), you can have two streets (classes or functions) with the same name, and reference each one directly.

namespace‘ is now a reserved word, usage as an identifier will create a Fatal Error while parsing. Namespaces allow the developer to manage naming scopes without using the long names each time the class is referred to, and solve the problem of shared globals space without making code unreadable. This is experimental and subject to changes.

Namespace must be the first statement in the script. Multiple namespaces can be declared per file, the suggested syntax is utilizing brackets. You can utilize base function names within your own namespace. In the occasion that you need to differentiate between a function in your namespace and one in the global scope, current namespace is accessed unless explicitly directed other wise.

<?php
namespace MyProject {
  function strlen($str) { return \AnotherProject\strlen($str).' ['.strrev($str).']'; }
 
  echo 'NS ['.__NAMESPACE__."]\n";         // NS [MyProject]
  echo strlen('foo')."\n";                 // Lenght is: 3 [oof]
}
 
namespace AnotherProject {
  function strlen($str) { return 'Length is: '.\strlen($str); }
 
  echo 'NS ['.__NAMESPACE__."]\n";         // NS [AnotherProject]
  echo strlen('foo')."\n";                 // Lenght is: 3
  echo \strlen('foo')."\n";                // 3
  echo namespace\strlen('foo')."\n";       // Lenght is: 3
  echo \MyProject\strlen('foo')."\n";      // Length is: 3 [oof]
  echo \AnotherProject\strlen('foo')."\n"; // Length is: 3
}
 
namespace {
  echo 'NS ['.__NAMESPACE__."]\n";         // NS []
  echo strlen('foo')."\n";                 // 3
  echo AnotherProject\strlen('foo')."\n";  // Lenght is: 3
  echo MyProject\strlen('foo')."\n";       // Lenght is: 3 [oof]
  echo \strlen('foo')."\n";                // 3
  echo \AnotherProject\strlen('foo')."\n"; // Lenght is: 3
  echo \MyProject\strlen('foo')."\n";      // Lenght is: 3 [oof]
  echo namespace\strlen('foo')."\n";       // 3
}
?>

Function, class and constant references inside a namespace refer to that namespace first and global scope next. Names that start with a backslash ‘\’ are always interpreted as fully qualified namespace. In the case that the class is in neither the current namespace nor the global scope, autoload is called. __autoload() will be passed the namespace name with the class name. The string passed to the autoload includes the fully qualified namespace for the call (IE: long\namespace\class_name.)

Note: An __autoload() declared inside of a namespace will not be called. For integration of an autoload in a namespace or class utilize spl_autoload_register.

As utilized in the preceding code segment, the namespace keyword provides the current namespace, as well as the new __NAMESPACE__ Magic Constant. Namespace also implements aliases via the use and AS construct (IE: use realy\long\ns\here AS a;.)

For more information check out Namespace on PHP.net

1 Response to “Namespaces”

  1. [...] the language, more information on select changes will be covered in future articles. An article on Namespaces has already been posted for more [...]

Leave a Reply