PHP 5.3 namespaces for the rest of us

According to the official documentation, PHP namespaces have been designed to prevent name collisions between classes from different packages and to avoid the use of very long names in the code to refer to classes or functions—nobody really wants to have to deal with something called Zend_Db_Adapter_Mysqli or PHPUnit_Framework_Constraint_IsInstanceOf, after all. This means that namespaces help a developer write code that is both more concise and clearer—a direction which is always an improvement towards expressiveness.

Within the PHP implementation of namespaces, these names will be ideally refactored to Zend\Db\Adapter\Mysqli and PHPUnit\Framework\Constraint\IsInstanceOf, where \ is the namespace separator. In the codebase, however, there will typically be very few references to these classes with their fully qualified name, because it is possible to import entire namespaces in a script and then use the class names directly, making the code easier to follow and unambiguous to write.

In fact, the definition of a namespace class itself does not contain its fully qualified name. For example, this would be the source file of an hypothetical MyLibrary\TypeOfComponents\MyClass class:

<?PHP
namespace MyLibrary\TypeOfComponents;
class MyClass
{
// ...
}

The convention when writing namespace-enabled code is that of creating a folder structure that reflects the individual components of a namespace (for example, MyClass would be in the MyLibrary/TypeOfComponents directory. This helps standardizing the autoloading process.

Read the full story PHP 5.3 namespaces for the rest of us | php|architect.

Introducing a Tool for Namespacing PHP5 Prefixed codebases

Ralph Schindler writes;
Hey All-

Over the past few days, I’ve been working on a tool that I think might help expedite the task of converting all of our code into a Namespaced codebase.  Currently, it’s been tested on simple components like Zend_Acl and Zend_Filter and over the course of the next few days, we’ll be applying it to some of the more complex components.

What it does..

This tool is a command line tool.  First, you may get it from my GitHub repository:

http://github.com/ralphschindler/PHPTools

git clone http://github.com/ralphschindler/PHPTools.git

-OR-

Just download the tar/gz/zip via the github interface.

Inside here you’ll find a bin/ library/ and test/ directory, the namespacer tool is located in this library.  To get the basic help screen, simply run ‘php bin/php-namespacer.php -h’.  This will give you a couple of available options to understand what the various command line switches do.  Personally, I create a link from somewhere in my path, usually $HOME/bin/php-namespacer to path/to/PHPTools/bin/php-namespacer.php, this makes it easier to run the command from anywhere in your filesystem.

To be able to get up and running with it quickly, I suggest running to tool in the following manner to get an idea of what it will produce:

bash> php-namespacer -l=../library/ -d=Zend/Acl -p=Zend -o=./tmp/ -m=./tmp/

Here are is what the above is going to do:

  • with the -l switch, you are telling it the location of a library this is typically the same directory you’d add to an include_path
  • with the -d switch, you are telling the tool that of all the classes it comes across in the library, only work on this particular directory
  • -p is the prefix we want to work on, in our case Zend/
  • -o is the directory where you want your new files written to
  • -m is the directory where an XML file will be produced of all the name translations that have taken place.

The full library is supplied so that the tool can iterate all files for class names that might be present inside the working directory’s files.

This is so that it knows what those translations will be once those components are eventually converted (assuming they are part of the same effort).

This tool will OPTIONALLY convert names in docblock ONLY if you have the pecl ext/docblock extension compiled and enabled (http://pecl.php.net/package/docblock).  Otherwise it will not touch the docblocks.

I have to admit that even though we are seeing some great results so far, this tool is still in it’s infancy.  We are opening it up to you for feedback and if you find it useful, to run it over your own code to see if it does the job of converting prefixed PHP5 code into namespaced PHP 5.3 code.

I’ll let everyone get a chance to run the tool and produce some output first before we start discussing the actual output and file contents.

For discussion purposes, we’ve been heavily looking at simpler components like Zend_Acl, Zend_Filter and Zend_Config- but don’t let that stop you from running it over other components or even your own code.

Let me know what you think!

Ralph Schindler