Is there any way how to avoid to using so often backslash ?
Now if i'm using namespace and calling something global inside it i have to use backslash:
namespace foo; $a = new \my\name(); // instantiates "my\name" class echo \strlen('hi'); // calls function "strlen" $a = \INI_ALL; // $a is set to the value of constant "INI_ALL"
in that case code inside namespace become really ugly, is there any way how to avoid that situation ???
The example was taken from that url: <a href="http://www.php.net/manual/en/language.namespaces.faq.php" rel="nofollow">http://www.php.net/manual/en/language.namespaces.faq.php</a>
But my problem in that, if I need call some built in class from namespace i have to use backslash in front of the name, can I somehow avoid it ?Answer1:
constants and functions from the global namespace do not have to be prepended with a backslash. PHP will fallback to the global namespace for those on it's own. There is a whole chapter in the PHP manual explaining this:<ul><li><a href="http://php.net/manual/en/language.namespaces.fallback.php" rel="nofollow">Using namespaces: fallback to global function/constant</a></li> </ul><blockquote>
Inside a namespace, when PHP encounters a unqualified Name in a class name, function or constant context, it resolves these with different priorities. Class names always resolve to the current namespace name. […] For functions and constants, PHP will fall back to global functions or constants if a namespaced function or constant does not exist.</blockquote>