Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can PHP namespaces contain variables?

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

like image 385
EmpireJones Avatar asked Mar 13 '11 03:03

EmpireJones


People also ask

Which types are supported in PHP namespaces?

A namespace can contain valid PHP code. Namespace affects following types of code: classes (including abstracts and traits), interfaces, functions, and constants.

What is a variable namespace?

A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.

How does namespaces work in PHP?

Namespaces are qualifiers that solve two different problems: They allow for better organization by grouping classes that work together to perform a task. They allow the same name to be used for more than one class.

What is the difference between namespace and use in PHP?

A namespace is a way of grouping identifiers so that they don't clash. Using a class implies that you can create an instance of that class, not true with namespaces. 2. You can use using-declarations with namespaces, and that's not possible with classes unless you derive from them.


1 Answers

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" 
like image 137
mario Avatar answered Sep 22 '22 18:09

mario