将 PHP 库迁移到命名空间的充分理由

发布于 2024-08-28 18:56:56 字数 273 浏览 2 评论 0原文

我有大量为 PHP 5.2.5 编写的对象库,并且我正在尝试权衡为命名空间改造它们的好处。目前我对服务器 PHP 版本没有任何担忧,因为任何相关机器都在我的控制之下,所以我不担心向后兼容性。就库的结构而言,我使用与 Zend Framework 相同的约定(Library_Module_Class_Name 例如),因此目前库内部没有任何命名冲突。我预计将这些类名的库和模块部分移动到命名空间。

也就是说,如果代码已经编写完毕,是否有充分的理由转移到命名空间?

谢谢, 乔

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, since 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑕疵 2024-09-04 18:56:56

如果您正在寻找技术原因,我认为没有任何理由可以证明大型代码移动的成本是合理的。据我所知,命名空间不会带来任何技术优势或性能优势。为一组结构良好的命名空间 PHP 代码编写像样的自动加载函数会更容易,但除此之外,我想不出有什么是开始转换的根本原因。如果有的话,命名空间解析可能会产生一点点开销(基准测试很容易看到,但我确信这是一个可以忽略不计的差异)。

话虽这么说,我最近开始更新一组旧代码以使用命名空间,并且我很享受它提供的组织功能。该代码倾向于更加冗长,但我在升级开始时将 IDE 切换到 Netbeans 6.8,并且自动完成功能解决了导入和引用命名空间的许多繁琐工作。实际上,命名空间支持给我留下了深刻的印象。这使得整个项目变得非常简单,而且实际上,我敢说,很有趣。

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.

乞讨 2024-09-04 18:56:56

是的,您的对象是一个对象,而不是 Namespace_Prepending_To_An_Object。难道您不想使用 Controller 而不是 Zend_Controller 吗?后者看起来很荒谬。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文