How to use same named class in PHP without namespacing?

回眸只為那壹抹淺笑 提交于 2020-01-24 10:42:08

问题


I have a problem, where I have two third party libraries classes that I have to extend and use together. But both have the same naming convention and two class name end up having the same name.

Since I cannot extend two classes, I don't know how to fix them, how to create a wrapper against one. I cannot use PHP Namespaces as the PHP version is just 5.2.10 and not 5.3.

What options I have?


回答1:


I think you can implement rpc like interface, for one class. For other you can extend and use. http://en.wikipedia.org/wiki/Remote_procedure_call




回答2:


That's the trouble when you don't have namespaces.

The only option you have is to go through the code of one or the other, and change all the class names. Perhaps add a prefix to both of them that makes it clear which one is which.




回答3:


This is exactly the problem that PHP namespaces are intended to solve. There is no clean way to do this pre-5.3. The best you can do is to rename one or both and go through the libraries correcting the calls. This is a pain, but, as they are separate libraries, this shouldn't be too difficult.

My other suggestion would be to make sure that installing 5.3 really is impossible. It is by far the best solution to your problem.



来源:https://stackoverflow.com/questions/4023360/how-to-use-same-named-class-in-php-without-namespacing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!