Search code examples
phpvariablesnamespaces

Can PHP namespaces contain variables?


Can PHP namespaces contain variables? If so, how can this be accomplished?


Solution

  • No. You can set a variable after declaring a namespace, but variables will always exist in the global scope. They are never bound to namespaces. You can deduce that from the absence of any name resolution descriptions in

    There would also be no allowed syntax to locate variables in a namespace.

    print \namespace\$var;      // syntax error
    
    print "${namespace\\var}";  // "unexpected T_NS_SEPARATOR"