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
87
votes
15 answers

How to get all class names inside a particular namespace?

I want to get all classes inside a namespace. I have something like this: #File: MyClass1.php namespace MyNamespace; class MyClass1() { ... } #File: MyClass2.php namespace MyNamespace; class MyClass2() { ... } #Any number of files and classes…
Pedram Behroozi
  • 2,447
  • 2
  • 27
  • 48
87
votes
6 answers

'SuppressMessage' for a whole namespace

I use underscores for my test methods for a better readability and I want to suppress FxCop errors/warnings for the whole test namespace. How can I achieve this? I played with GlobalSuppressions.cs but nothing worked: [module:…
timmkrause
  • 3,367
  • 4
  • 32
  • 59
86
votes
2 answers

PHP how to import all classes from another namespace

I'm implementing namespaces in my existing project. I found that you can use the keyword 'use' to import classes into your namespace. My question is, can I also import all the classes from 1 namespace into another. Example: namespace foo { …
Rob
  • 2,466
  • 3
  • 22
  • 40
84
votes
1 answer

PHP : 'use' inside of the class definition

Recently I came across a class that uses use statement inside of the class definition. Could someone explain what exactly does it do - as I can't find any information about it. I understand that it might be a way of moving it away form a global…
Sebastian Sulinski
  • 5,815
  • 7
  • 39
  • 61
84
votes
5 answers

Using :: (scope resolution operator) in C++

I am learning C++ and I can never tell when I need to use :: . I do know that I need to use std:: in front of cout and cin. Does this mean that inside of the iostream file the developers that created it made a namespace called std and put the…
foobar5512
  • 2,470
  • 5
  • 36
  • 52
83
votes
7 answers

Visual Studio 2010 suddenly can't see namespace?

My C# WinForms solution has two projects. A DLL which is the main project I'm working on, and an executable WinForms I call "Sandbox" so that I can compile/run/debug the DLL easily in one go. I'm working in .Net 4.0 for both projects. Everything was…
Ozzah
  • 10,631
  • 16
  • 77
  • 116
81
votes
7 answers

Partial class in different namespaces

Can I create partial class in different namespaces? Will it work correct? e.x.: class1.cs namespace name1 { public partial class Foo { Bar1(){ return 10; } } } class2.cs namespace name1.name2 { public…
RAMe0
  • 1,415
  • 2
  • 19
  • 31
81
votes
1 answer

Rails 4: organize rails models in sub path without namespacing models?

Would it be possible to have something like this? app/models/ app/models/users/user.rb app/models/users/education.rb The goal is to organize the /app/models folder better, but without having to namespace the models. An unanswered question for Rails…
Rubytastic
  • 15,001
  • 18
  • 87
  • 175
80
votes
2 answers

How to check if class exists within a namespace?

I've got this: use XXX\Driver\Driver; ... var_dump(class_exists('Driver')); // false $driver = new Driver(); // prints 123123123 since I put an echo in the constructor of this class exit; Well... this behaviour is quite…
Taruo Gene
  • 1,023
  • 1
  • 9
  • 16
79
votes
9 answers

C++ namespaces advice

I'm just teaching myself C++ namespaces (coming from a C# background) and I'm really starting to think that even with all the things that C++ does better than most other languages, nested namespaces isn't one of them! Am I right in thinking that in…
Adam Naylor
  • 6,172
  • 10
  • 49
  • 69
79
votes
7 answers

socket.error:[errno 99] cannot assign requested address and namespace in python

My server software says errno99: cannot assign requested address while using an ip address other than 127.0.0.1 for binding. But if the IP address is 127.0.0.1 it works. Is it related to namespaces? I am executing my server and client codes in…
user2833462
  • 805
  • 1
  • 7
  • 6
79
votes
26 answers

error C2065: 'cout' : undeclared identifier

I am working on the 'driver' part of my programing assignment and i keep getting this absurd error: error C2065: 'cout' : undeclared identifier I have even tried using the std::cout but I get another error that says: IntelliSense: namespace "std"…
Wallter
  • 4,275
  • 6
  • 29
  • 33
78
votes
10 answers

What are XML namespaces for?

This is something that I always find a bit hard to explain to others: Why do XML namespaces exist? When should we use them and when should we not? What are the common pitfalls when working with namespaces in XML? Also, how do they relate to XML…
Doron Yaacoby
  • 9,412
  • 8
  • 48
  • 59
75
votes
1 answer

createElement vs. createElementNS

What's the real difference between those two? I mean real, essential difference. What's the future holding for regular createElement? Svg is xml, not html. I get that. So we use createElementNS(ns_string, 'svg') And then setAttributeNS(null,,).…
CoR
  • 3,826
  • 5
  • 35
  • 42
75
votes
2 answers

XElement namespaces (How to?)

How to create xml document with node prefix like: When I try to run…
Edward83
  • 6,664
  • 14
  • 74
  • 102