Can PHP namespaces contain variables?

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

  • FAQ: things you need to know about namespaces (PHP 5 >= 5.3.0)

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"

Leave a Comment