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
6
votes
4 answers

C++ using namespace statement

namespace MyNamespace { static void foo1() { } } using namespace MyNamespace; class MyClass { void foo2() { ::foo1(); } }; The scope resolution operation :: means using method in the global namespace. Here we…
softempire
  • 135
  • 7
6
votes
4 answers

Best practice when creating reusable corporate namespace

Alright, this has been on my mind for a while now. I'm in the process of creating a reusable corporate namespace(class library) for all common middle-tier objects. This assembly can then be referenced by any of our developers during the development…
TCNS-Bob
  • 63
  • 6
6
votes
2 answers

C#: How to convert a Website project to a Web Project

UPDATE: All of these problems was for converting a Website application to a Web Project in Visual Studio. Once I dug in and I found the solution, as I marked as answer below. Basically, if you are wanting to upgrade/move your Website project to a…
eduncan911
  • 17,165
  • 13
  • 68
  • 104
6
votes
5 answers

Confusion about C# Namespace Structure

I am little confused about the structure of Namespace in C# I'm new to C# and WPF When i create a new project of WPF, by default these Namespaces are included at the top using System; using System.Collections.Generic; using System.Linq; using…
ammar26
  • 1,584
  • 4
  • 21
  • 41
6
votes
2 answers

PHP namespaces: equivalent to C# using

What is the equivalent of C#'s using Name.Space; statement to make all classes of that namespace available in the current file? Is this even possible with PHP? What I'd like (but does not work):
knittl
  • 246,190
  • 53
  • 318
  • 364
6
votes
2 answers

Old code still being executed in ipython after files have been modified

In file1.py: def foo(): import file2 print "I'm the old file1.py" file2.bar() if __name__ == '__main__': foo() In file2.py print "I'm the old file2.py" def bar(): print "I'm in the old file2.bar()" On line 5 of the…
wim
  • 338,267
  • 99
  • 616
  • 750
6
votes
2 answers

ASP.NET MVC: Namespaces in routes

I have a small problem, I can't find any documentation on the namespaces parameter for MapRoute. Can anyone explain how I should use that? I want to map ~/Controllers/Projects/ProjectController.cs to this url ~/Projects/ but I also have other…
Kim Johansson
  • 372
  • 2
  • 8
6
votes
2 answers

PHP namespaced function best practices

I have a few general use functions that do not really make sense in any class as static methods. I would like to encapsulate them under a namespace so there are no conflicts with functions defined in the global scope. For my namespaced classes, I…
rr.
  • 6,484
  • 9
  • 40
  • 48
6
votes
3 answers

I can't add reference to namespace system.windows.controls in a library project

I'm trying to add a reference to the namespace System.Windows.Controls in a library project but i can't find it in the list. Does anybody know what is going on? i'm using 4.0. thanks.
Ziad
  • 187
  • 2
  • 3
  • 13
6
votes
1 answer

How do `ip netns` and `unshare` save their persistent network namespaces? Can they use each others?

To make a persistent namespace with unshare you use the syntax: touch /root/mynetns1 unshare --net==/root/mynetns1 To make a persistent namespace with ip you use the syntax: ip netns add mynetns2 The ip command does not list or can access the…
6
votes
2 answers

Namespace interface

I have struct: /VBAL/ /VBAL/Interface/ /VBAL/Interface/Named.php .... /VBAL/Component.php Component.php: namespace JV\VBAL; class Component implements \JV\VBAL\Interface\Named {} Named.php: namespace JV\VBAL\Interface; interface Named {} But…
ajile
  • 666
  • 1
  • 10
  • 18
6
votes
3 answers

How to Unit Test namespaced models

In my Rails app, I make heavy use of subdirectories of the model directory and hence of namespaced models - that is, I have the following directory heirarchy: models | +- base_game | | | +- foo.rb (defines class BaseGame::Foo) | +- expansion | …
Chowlett
  • 45,935
  • 20
  • 116
  • 150
6
votes
1 answer

Do C++ modules make unnamed namespaces redundant?

C++20 introduced modules. Any symbol that is not exported in a module has module-internal linkage. While unnamed namespaces provide a mechanism to make definitions inside an unnamed namespace have file-internal linkage. Does this mean unnamed…
John Z. Li
  • 1,893
  • 2
  • 12
  • 19
6
votes
2 answers

How best for a Firefox extension to avoid polluting the global namespace?

I've been reading up on global namespace pollution when developing an extension for Firefox, and I want to avoid it as much as possible in my extension. There are several solutions, but generally, the solutions seem to center around only declaring…
Jez
  • 27,951
  • 32
  • 136
  • 233
6
votes
1 answer

Ruby Thor based executable with namespaces

Is it possible to create a Thor based Ruby executable that accepts namespaces? To allow, for example, the following from the commandline: ./thorfile greet:formal Given I have the following thorfile: #!/usr/bin/env ruby require 'rubygems' require…