Reputation: 21174
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');
then Netbeans will suggest changing it to:
\str_replace('stuff');
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.
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.
Upvotes: 14
Views: 3264
Reputation: 144
According to this article, there is another technical reason why this is done: A small number of PHP internal functions have compiler optimized versions that avoid a lot of the internal overhead of an internal function call in the PHP engine.
source: https://tideways.com/profiler/blog/compiler-optimized-php-functions
Upvotes: 0
Reputation: 46
What are compiler optimized internal PHP functions and should you import them via use statement?
Curious beings as we are, you might wonder as did I: Why are they doing this? Do function calls not automatically fall back into the global namespace? Yes they do, you don’t technically need to use function or prefix with the global namespace separator.
But there is another technical reason why open-source libraries do this: A small number of PHP internal functions has compiler optimized versions that avoid a lot of the internal overhead of an internal function call in the PHP engine. You can find these functions in the zend_compile.c file of PHP.
Upvotes: 0
Reputation: 7194
Netbeans is simply not following the documented rules of resolving namespaces as documented:
In other words, your code should be considered idiomatic based on the documented rules.
Upvotes: 0
Reputation: 316969
There is no such recommendation in
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.
You might want to ask on the Netbeans Mailing List for more details about why your IDE suggests this.
Upvotes: 5
Reputation: 18859
Overzealous for sure, but I can't reproduce this is Netbeans 7.0.1, with PHP Plugin 1.17.1. It's not the convention, anyway, and I would not consider it a best practice at all.
Upvotes: 1