开发者

PHP strategies for retrofitting a namespace around 3rd party code

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 r开发者_如何转开发etrofit 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 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜