Questions tagged [namespaces]

A namespace is a container that provides context for identifiers, within which names are unique.

A namespace is a container that provides context for identifiers, within which names are unique. In many implementations, identifiers can be disambiguated between namespaces by prepending the identifier with the namespace, separated by a delimiter such as a period (.) in and , double-colon (::) in or backslash (\) in .

For many programming languages, namespace is a context for their identifiers. In an operating system, an example of namespace is a directory. Each name in a directory uniquely identifies one file or subdirectory, but one file may have the same name multiple times.

As a rule, names in a namespace cannot have more than one meaning; that is, different meanings cannot share the same name in the same namespace. A namespace is also called a context, because the same name in different namespaces can have different meanings, each one appropriate for its namespace.

Following are other characteristics of namespaces:

  • Names in the namespace can represent objects as well as concepts, be the namespace a natural or ethnic language, a constructed language, the technical terminology of a profession, a dialect, a sociolect, or an artificial language (e.g., a programming language).
  • In the Java programming language, identifiers that appear in namespaces have a short (local) name and a unique long "qualified" name for use outside the namespace.
  • Some compilers (for languages such as C++) combine namespaces and names for internal use in the compiler in a process called name mangling.

PHP

Namespaces were introduced into PHP from version 5.3 onwards. In PHP, a namespace is defined with a namespace block.

namespace phpstar {
    class fooBar {
        public function foo() {
            echo 'hello world, from function foo';
        }

        public function bar() {
            echo 'hello world, from function bar';
        }
    }
}

XML

In XML, the XML namespace specification enables the names of elements and attributes in an XML document to be unique. Using XML namespaces, XML documents may contain element or attribute names from more than one XML vocabulary.

Python

In Python, namespaces are defined by the individual modules, and since modules can be contained in hierarchical packages, then name spaces are hierarchical as well. In general when a module is imported then the names defined in the module are defined via that module's name space, and are accessed in from the calling modules by using the fully qualified name.

.NET

All .NET Framework classes are organized in namespaces. When referencing a class, one should specify either its fully qualified name, which means namespace followed by the class name,

C++

In C++, a namespace is defined with a namespace block.

namespace abc {
    int bar;
}
12341 questions
75
votes
5 answers

Namespace constant in C#

Is there any way to define a constant for an entire namespace, rather than just within a class? For example: namespace MyNamespace { public const string MY_CONST = "Test"; static class Program { } } Gives a compile error as…
Paul Michaels
  • 16,185
  • 43
  • 146
  • 269
75
votes
6 answers

Why can't we declare a namespace within a class?

Declaring a class within a class is valid. (Nested classes) Declaring a namespace within a class is invalid. The question is: is there any good reason (other than c++ grammar/syntax problems) to forbid the declaration of a namespace within a class…
Drax
  • 12,682
  • 7
  • 45
  • 85
72
votes
8 answers

Can PHP namespaces contain variables?

Can PHP namespaces contain variables? If so, how can this be accomplished?
EmpireJones
  • 2,936
  • 4
  • 29
  • 43
71
votes
4 answers

Uses of unnamed namespace in C++

When would one use unnamed namespace in C++ ? Is it better in any sense than a free standing function? Also, should it be used only in source file and not in header file?
Asha
  • 11,002
  • 6
  • 44
  • 66
71
votes
5 answers

Why is my log in the std namespace?

In the code below, I define a trivial log function. In main I try not to call it; I call std::log. Nevertheless, my own log is called; and I see "log!" on screen. Does anyone know why? I use G++ 4.7 and clang++ 3.2. #include #include…
user2023370
  • 10,488
  • 6
  • 50
  • 83
71
votes
5 answers

Global scope vs global namespace

I saw usages of these two phrases: global scope and global namespace. What is the difference between them?
scdmb
  • 15,091
  • 21
  • 85
  • 128
70
votes
7 answers

Is there any way to make Visual Studio stop indenting namespaces?

Visual Studio keeps trying to indent the code inside namespaces. For example: namespace Foo { void Bar(); void Bar() { } } Now, if I un-indent it manually then it stays that way. But unfortunately if I add something right before void…
Andreas Bonini
  • 44,018
  • 30
  • 122
  • 156
69
votes
1 answer

How to use namespaces with import in TypeScript

I have two classes in two separate files and one extends from another. The base class contains some import statements using node modules. It is unclear to me why the derived class (which is in a separate file) does not recognize the base…
gevik
  • 3,177
  • 4
  • 25
  • 28
69
votes
5 answers

Autoloading classes in PHPUnit using Composer and autoload.php

I have just installed PHPUnit version 3.7.19 by Sebastian Bergmann via Composer and have written a class I would like to unit test. I would like to have all my classes autoloaded into each unit test without having to use include or require at the…
Jasdeep Khalsa
  • 6,740
  • 8
  • 38
  • 58
69
votes
3 answers

XSLT with XML source that has a default namespace set to xmlns

I have an XML document with a default namespace indicated at the root. Something like this: 1234 The XSLT to parse the XML does not work as…
Larry
  • 989
  • 4
  • 12
  • 25
68
votes
3 answers

Could not find 'WindowsFormsApplication1.Program' specified for Main method after renaming name space

I have a C# app that had the default namespace WindowsFormsApplication1. I decided that I would like to use a different namespace so I renamed WindowsFormsApplication1. Now when I compile I get the error: Could not find…
Mausimo
  • 8,018
  • 12
  • 52
  • 70
68
votes
6 answers

What exactly does "import *" import?

In Python, what exactly does import * import? Does it import __init__.py found in the containing folder? For example, is it necessary to declare from project.model import __init__, or is from project.model import * sufficient?
ensnare
  • 40,069
  • 64
  • 158
  • 224
67
votes
10 answers

Should I stop fighting Visual Studio's default namespace naming convention?

I'm working on an MVVM project, so I have folders in my project like Models, ViewModels, Windows, etc. Whenever I create a new class, Visual Studio automatically adds the folder name to the namespace designation instead of just keeping the…
devuxer
  • 41,681
  • 47
  • 180
  • 292
66
votes
14 answers

Using Xpath With Default Namespace in C#

I've got an XML document with a default namespace. I'm using a XPathNavigator to select a set of nodes using Xpath as follows: XmlElement myXML = ...; XPathNavigator navigator = myXML.CreateNavigator(); XPathNodeIterator result =…
macleojw
  • 4,113
  • 10
  • 43
  • 63
66
votes
3 answers

How to get only class name without namespace

There is a class like this. module Foo class Bar end end And I want to get the class name of Bar without Foo. bar = Foo::Bar.new bar.class.to_s.match('::(.+)$'){ $1 } I could get the class name by this code, but I don't think this is a best…
ironsand
  • 14,329
  • 17
  • 83
  • 176