Tuesday, September 01, 2009

Introspection of php namespaces

Php 5.3 introduced the use of namespaces, a mean to organize classes and functions in packages with a common base name. Some php features that works on classes, such as Class/Object functions and variable class names, still work without confusion with a bit of attention on the data used.

Php namespaces organizes classes in a naming scheme which uses the standard separator \ to build groups and subgroups of elements, to shorten long class names and preventing naming conflicts. The classic name convention of My_Class is now becoming My\Class. Some functionalities are affected by the instantiation and I made a list of what changes when introducing the namespace keyword in your php source files.

Strings containing class names
Backslash (\ character) is the chosen separator for namespaces usage. This means that when used in a php single- or double-quoted string, it should be backslashed another time to prevent parsing: "My\\NameSpace\\Class" is a valid example.

Class/Object functions
Remember that this functions requires and returns fully qualified class names. In this group of function we find class_exists() and get_class(). Their argument is required to be a full class name (such as My\Class) and the returned values will contain full class name, for instance a call to get_declared_classes().
This is straightforward if you consider the namespace name resolving process as done at the compile time: since an argument to this functions is a literal string or a variable, it cannot be considered as a base class name of some namespace since this will involve php namespace resolutioon parsing string parameters, while it is built to work on code.
Finally, fully qualified class names never have a leading backslash. They are as simply as My\Class.

autoload() function and standard autoloaders pushed to spl_autoload() should work with the fully qualified class name. This is the same argument discussed on Class/Object functions.

Variable as a class name
This case is a bit trickier: again namespaced elements are resolved at compile time, so the instruction new $class is executed in the subsequent global environment, and assuming you have a fully qualified name in $class you should not prepend a backslash to make it work. Always use fully qualified names in code like this: tying $class to the executor specific namespace is not possible.
$resolvedClassName = 'NameSpace\\Other\\ClassName';
$object = new $resolvedClassName;
Standard Php Library (SPL)
Spl classes now reside in the global namespace. So every time you wrote a namespaced file, for instance a class one, you should refer to ArrayObject, Iterator etc. as \ArrayObject and \Iterator, or write a use instruction at the top of your file along with the namespace declaration.
Except if you are instantiating them via a variable class name, like in the last paragraph. If you prepend a backslash, however, it really doesn't matter and it will be stripped.

I hope these tips will help you take advantage of namespace features without wondering about what type of names should be used in coding.

No comments: