how to call global functions classes from namespace PHP

前端 未结 1 643
粉色の甜心
粉色の甜心 2021-01-18 04:41

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:



        
1条回答
  •  粉色の甜心
    2021-01-18 05:38

    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:

    • Using namespaces: fallback to global function/constant

    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.

    0 讨论(0)
提交回复
热议问题