PHP Packages and Namespaces

PHP Packages and Namespaces

Package is a set of PHP classes grouped in some manner .Many developers use file system to organise their applications because PHP has no native concept of a package until PHP 5.3 .With PHP 5.3 ,namespaces become part of PHP .I know that most of us think many times before using namespaces in any commercial code .you know ,not all servers support PHP 5.3 but this will be for a while .

Creating Namespaces

Namespaces like a box in which you can place your classes ,variables and functions .And then you can simply import namespace or reference to it from outside .Conside the following example :

 namespace utils;

 class fileManager {

  static function hello() {
   print 'Hello World';
  }

 }

For furthur organisation to your projects ,you can use nested namespaces .To do this ,use a backslash to divide each part like following :

  namespace com\myapp\util\routing;

 class fileManager {

  static function hello() {
   print 'Hello World';
  }

 }

Accessing Namespaces

If you are calling the class from within the namespace ,you should call it directly

 namespace com\myapp\util\routing;

 class fileManager {

  static function hello() {
   print 'Hello World';
  }

 }
 //access from inside namespace
fileManager::hello();

If you need to access class from outside namespace ,you could do the following

//tut-1.php file
 namespace com\myapp\util\routing;

 class fileManager {

  static function hello() {
   print 'Hello World';
  }

 }

 //tut-2.php file
 require_once 'tut-1.php';
 com\myapp\utils\routing\fileManager::hello();

If you need to access class from outside but inside another namespace ,you could do this

 //tut-2.php file
 namespace com\myapp\util;
 require_once 'tut-1.php';
 //relative call
 routing\fileManager::hello();
 //absolute call
 \com\myapp\util\routing\fileManager::hello();

As you can see in previous example ,I add leading backslash to tell PHP to begin search from root and not relative to current namespace .

If there is many namespaces to use in current file or current namespace ,you don’t have to use nested syntax for each call .But , you can use use keyword to import other namespaces like that

 namespace com\myapp\util;
 use com\myapp\util\routing\fileManager;
 require_once 'tut-1.php';
 fileManager::hello();

It is worth noting that ,any possibilities that your class name may collide .You could use as keyword .

 //tut-1.php file
 namespace com\myapp\util\routing;

 class fileManager {

  static function hello() {
   print 'Hello World';
  }

 }
 
//tut-2.php file
namespace com\myapp\util;
use com\myapp\util\routing\fileManager as GfileManager;
require_once 'tut-1.php';

 class fileManager {

  static function hello() {
   print 'Hello Worlde';
  }

 }

 GfileManager::hello();
 fileManager::hello();

If you need to access class in a non-namespaced file from within namespaced file ,you could use the following code .

 //tut-1.php file
 class fileManager {

  static function hello() {
   print 'Hello World';
  }

 }
 
//tut-2.php file
namespace com\myapp\util;
require_once 'tut-1.php';

 class fileManager {

  static function hello() {
   print 'Hello World';
  }

 }

 //access local class
 fileManager::hello();
 //access global class
 \fileManager::hello();

You could also combine multiple namespaces in the same file by wrapping namespace body by braces like that

 namespace com\myapp\util1 {

  class fileManager {

   static function hello() {
    print __NAMESPACE__;
   }

  }

 }

 namespace com\myapp\util2 {

  class fileManager {

   static function hello() {
    print __NAMESPACE__;
   }

  }
 //access file manager in current namespace
 fileManager::hello();
 //access another namespace
 \com\myapp\util1\fileManager::hello();
 }

Another something about namespaces and it is worh using is __NAMESPACE__ constant .This constant output the current namespace you called from .