开发者

Good reasons to migrate PHP libraries to namespaces

开发者 https://www.devze.com 2022-12-25 20:14 出处:网络
I have a significant number of object libraries written for PHP 5.2.5, and I\'m trying to weigh the benefits of retrofitting them for namespaces. I don\'t have any concerns about the server PHP versio

I have a significant number of object libraries written for PHP 5.2.5, and I'm trying to weigh the benefits of retrofitting them for namespaces. I don't have any concerns about the server PHP version at the moment, s开发者_StackOverflow中文版ince any relevant machines are under my control, so I'm not worried about backwards compatibility. As far as the structure of the libraries, I use the same convention as Zend Framework, (Library_Module_Class_Name e.g.) so I don't currently have any naming conflicts internal to the libraries. I'd anticipate moving the Library and Module parts of those classnames to namespaces.

That said, if the code is already written, is there any good reason to move over to namespaces?

Thanks, Joe


If you're looking for technical reasons, I don't think there's anything to justify the cost of a large code move. As far as I'm aware, namespaces don't confer any kind of technical advantage or performance benefit. It's easier to write decent autoloading functions for a well-structured set of namespaced PHP code, but other than that, I can't think of anything that would be a ground-shaking definitive reason to start converting. If anything, there's probably a tiny bit of overhead for namespace resolution (benchmarks would be neat to see, but I'm sure it's a negligible difference).

That being said, I've recently started updating an old set of code to use namespaces, and I'm enjoying the organizational capability that it provides. The code has a tendency to be a bit more verbose, but I switched IDEs to Netbeans 6.8 at the start of the upgrade, and the autocomplete takes care of a lot of the tediousness of importing and references namespaces. The namespace support impressed me, actually. It's made the whole project quite easy, and actually, dare I say it, fun.


Yes, your object is an Object not a Namespace_Prepended_To_An_Object. Wouldn't you rather work with a Controller rather than a Zend_Controller? The latter just looks ridiculous.

0

精彩评论

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

关注公众号