I\'m attempting to automate the removal of namespaces from a PHP class collection to make them PHP 5.2 compatible. (Shared hosting providers do not fancy rogue PHP 5.3 insta
In an existing question on migration of namespaces to pseudo namespaced code I already introduced a conversion tool I have written as part of a larger project. I haven't maintained this project anymore since that point, but as far as I remember the namespace replacements did work. (I may reimplement this project using a proper parser at some point. Working with plain tokens has proven to be quite a tedious task.)
You will find my implementation of namespace -> pseudo-namespace resolution in the namespace.php. I based the implementation on the namespace resolution rules, which will probably be of help for you, too.
To make this a less blatant readmycodez answer, here the basic steps the code does:
self
and parent
classes, nor the true
, false
and null
constants.use
list):
class
, check whether the identifier is an alias and if it is, replace it with the aliased class.class
prepend the current namespace, replace all NS separators with underscores and end this algorithm.assumeGlobal
flag is set. Instead I always insert the dynamic dispatch code.)Additional note: Don't forget that one can also write namespace\some\ns
. I resolve these constructs in the NS function (which is also responsible for finding namespace declarations).