PHP Classes

NamespaceSim: Emulate class namespaces under PHP 5

Recommend this page to a friend!
  Info   View files View files (15)   DownloadInstall with Composer Download .zip   Reputation   Support forum (5)   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 68%Total: 1,772 All time: 2,209 This week: 206Up
Version License PHP version Categories
nsim 0.5.0GNU Lesser Genera...5.1PHP 5, Language, Code Generation


This package can be used to emulate class namespaces under PHP 5.

It parses PHP code to extract namespace definition statements. The code is rewritten to emulate the namespace support in a way similar to the Java language namespace support.

This package implements new commands named package and import. The package command defines the package that a class belongs. The package name is prepended to the class names using PEAR class naming and directory conventions.

The import command indicates that the package should be imported for use in the PHP script on which the import command is used. Classes that are not found in the current script are looked in the imported namespaces.

Error mapping has been added since 0.4.0, which requires that caching is enabled. This features makes php errors in rewritten files point to the right place in the original file.

As of version 0.5.0 nsim supports namespaces in:
* class definition (w/inheritance & interfaces implementation)
* interface definition (w/inheritance)
* static class vars & functions
* class constants
* instanceof operator
* type hinting

Innovation Award
PHP Programming Innovation award nominee
July 2007
Number 4

Prize: One downloadable copy of Komodo Pro
Namespaces is one of the features that was left out of PHP 5. It can help developers mix classes that have the same name but are from different packages.

Fortunately, namespaces are finally being introduced in PHP 6. However, PHP 5 users will not be able to benefit from that feature.

This package provides an alternative implementation that works on PHP 5. It parses the code of each class script and rewrite it to implement namespaces as prefixes of the class names.

Manuel Lemos
Picture of Martin Alterisio
Name: Martin Alterisio <contact>
Classes: 5 packages by
Country: Argentina Argentina
Age: 41
All time rank: 7348 in Argentina Argentina
Week rank: 245 Up2 in Argentina Argentina Up
Innovation award
Innovation award
Nominee: 5x


To be able to use the rewritten file caching and error mapping, there must be a folder named "nsimcache" where php should be able to write new files and folders (grant write permission on this folder).

  Files folder image Files  
File Role Description
Files folder imageNamespaceSim (8 files)
Files folder imageNotSimulated (1 file)
Files folder imageTestNamespace (2 files)
Files folder imageTestNamespace2 (1 file)
Accessible without login Plain text file example.nsim.php Example nsim example file
Accessible without login Plain text file README Doc. Readme file
Accessible without login Plain text file test.php Example test script

  Files folder image Files  /  NamespaceSim  
File Role Description
  Plain text file ClassConstantFacade.php Class a facade to access a class constants
  Plain text file ErrorMap.php Class nsim source file error mapping
  Plain text file Executer.php Class nsim source file executer
  Plain text file FindClassFacade.php Class a facade to find a real class name when using instanceof operator
  Plain text file Parser.php Class nsim source file parser
  Plain text file Rewriter.php Class nsim source file rewriter
  Plain text file StaticAccessFacade.php Class a facade to access a class static vars
  Plain text file UnexpectedToken.php Class parser unexpected token exception

  Files folder image Files  /  NotSimulated  
File Role Description
  Accessible without login Plain text file TestClass3.php Example access to nonsim'ed from nsim'ed example class

  Files folder image Files  /  TestNamespace  
File Role Description
  Accessible without login Plain text file TestClass.nsim.php Example nsim'ed example class
  Accessible without login Plain text file TestInterface.nsim.php Example nsim'ed example interface

  Files folder image Files  /  TestNamespace2  
File Role Description
  Accessible without login Plain text file TestClass2.nsim.php Example nsim'ed example class

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:2,209
This week:206Up
 User Ratings  
 All time