tags:

views:

51

answers:

2

I'm attempting to utilize a 3rd-party PHP library in a semi-developed web project. Unfortunately, as I am beginning to use the 3rd party code, I'm realizing that there is a number of variable and class name collisions. I was curious to know if there existed a strategy to retrofit a namespace around this new code.

...and yes, there's equal blame to be passed onto myself for not utilizing namespaces, as well.

Off the top of my head I am considering editing every .php file and adding:

namespace facePalm;

But I was curious to know if there happened to be a more elegant solution. Especially as additional revisions of the code are released, I would rather not have to touch & edit 30+ files.

A: 

Is the third party code subject to ongoing maintenance? If so, you'll want to work around whatever structure and conventions it has. In which case, doing a global search and replace on your names is the most expedient solution. For only 30 files, this is manageable in a day or two. You'll easily age an extra couple years per year if you don't.

wallyk
Updates to their code are anticipated. And yes, I had previously been manually resolving any naming collisions-- though I now feel dirty. In essence, "manageable in day or two" is too long for this lazy coder.
MikeHerrera
If the third party code worked without any namespace, putting it inside a namespace "ThirdParty" should be as easy as adding "namespace ThirdParty;" at the top of each file of the third-party script. Do a little script for it and you won.
Savageman
+1  A: 

A namespace is set for one file only, and a file can only contain 1 namespace. I'm afraid you don't have other solutions to do it. [edit] I just checked and one file can contain multiple namespaces. But you can't have a namespace in a file without defining it at the top of it.

Savageman
...combined with your comment, I found this to work. Adding a namespace to each file was sufficient in my specific case.
MikeHerrera