我应该在PHP中为全局函数命名空间吗? [英] Should I namespace global functions in PHP?

查看:82
本文介绍了我应该在PHP中为全局函数命名空间吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中,我在类级别使用名称空间.但是,Netbeans也不断告诉我我的全局函数的名称空间.例如,如果我输入

In PHP, I'm using namespaces at the class level. However, Netbeans keeps telling me namespace my global functions as well. For example, if I type

str_replace('stuff');

然后Netbeans建议将其更改为:

then Netbeans will suggest changing it to:

\str_replace('stuff');

这是PHP的建议,还是Netbeans太过热心?我无法在PHP文档中找到任何说明这两种方式的信息.

Is this a PHP recommendation, or just Netbeans being overzealous? I haven't been able to find anything in the PHP documentation that says either way.

我看不到它在代码中引起任何问题.但是,仅仅忽略Netbeans却不知道为什么首先推荐它是一种错误的感觉.但是仅仅改变我的编码实践而不知道这样做是正确的感觉也不对.

I can't see it causing any problems in the code. However, it feels wrong to just ignore Netbeans without knowing why it recommends it in the first place. But nor does it feel right just to change my coding practice without knowing it's the right thing to do.

推荐答案

也没有必要使用全局标识符,因为当当前名称空间中没有该名称的函数时,PHP将回退到全局函数定义.

Nor is it necessary to use the global identifer since PHP will fallback to the global function definition when there is no function of that name in the current namespace.

话虽如此,添加标识符的唯一原因是使其更加明确,即当有人向当前名称空间中添加相同名称的函数时,您想使用实际的全局事物来防止代码行为的意外更改.

With that said, the only reason to add the identifier is to make it more explicit that you want to use the actual global thing to prevent accidental changes in code behaviors when someone adds a function of the same name into the current namespace.

您可能想在 Netbeans邮件列表中询问有关为什么您的原因的更多详细信息IDE建议这样做.

You might want to ask on the Netbeans Mailing List for more details about why your IDE suggests this.

这篇关于我应该在PHP中为全局函数命名空间吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆