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
2 answers

Check if an object's type is from a particular namespace

Is it possible to check if the object's type is a part of a particular namespace from C# code? If yes, how? I need to check if e.OriginElement as FrameworkElement is one of the MS.Internal controls.
Maxim V. Pavlov
  • 10,303
  • 17
  • 74
  • 174
6
votes
4 answers

Hiding variables in a namespace C++

I have a function which aims to perform a recursive calculation. If my function is programmed recursively, it takes too long to compute. Therefore, I perform memoization by storing intermediate results in an array. During the execution of my…
RogerDorsey
  • 93
  • 2
  • 4
6
votes
3 answers

How to get the namespace of a node in xslt?

I'm propably doing something stupid here, I bet there is an easier way... I need to access namespace of a node. Elements in my xml looks for example like this: Then in my xslt I access this elements with: …
aurel
  • 1,117
  • 2
  • 11
  • 24
6
votes
4 answers

Cannot use a Namespaced Class inside another namespace in PHP

I am still having trouble with PHP5 Namespaces. I have a namespace called Project and I am trying to access a class called registry inside of this Project namespace that has a namespace of Library so at the top of the file that is a Project…
JasonDavis
  • 48,204
  • 100
  • 318
  • 537
6
votes
1 answer

C++ namespace and static variables

I have a requirement where a (const) variable should be available throughout an entire cpp which consists of several classes. I have decided to use a namespace to solve the problem, but unsure about the following: Do I need to define this variable…
s.d
  • 87
  • 2
  • 6
6
votes
1 answer

define a function in a specific namespace

I have created a package 'mypackage' (with a namespace 'mypackage' attached) In this package there is a function that I can call either with 'myfunction' or 'mypackage::myfunction' Now I want to replace myfunction by another version (updated). I…
RockScience
  • 17,932
  • 26
  • 89
  • 125
6
votes
6 answers

What does prepending "::" to a function call do in C++?

Possible Duplicate: What is the meaning of prepended double colon “::” to class name? I have been looking at a legacy C++ code and it had something like this: ::putenv(local_tz_char); ::tzset(); What does this syntax of prepending "::" to the…
DVK
  • 126,886
  • 32
  • 213
  • 327
6
votes
1 answer

How to change a Lift project root package?

A standard "Symply Lift" RestHelper example project features code, code.lib and code.model and bootstrap.liftweb.Boot namespaces. I've changed those to mycompany.myproject.code e t.c. Now the project compiles ok and Jetty starts just fine but I get…
Ivan
  • 63,011
  • 101
  • 250
  • 382
6
votes
3 answers

Hiding templated helper function - static members or unnamed namespace

I'm trying to write a library where I have some templated functions, some of which are helper functions so I don't want my users to have access to them. Some basic code might be //mylib.h namespace myfuncs { template void helper (T…
Phil Rosenberg
  • 1,597
  • 1
  • 14
  • 22
6
votes
1 answer

Getting the value of dc:creator using SQL XML

I am unsure how to get the value of dc:creator from an RSS-feed using SQL. This is my xml/rss-feed: Foobar RSS http://www.foobar.com/ …
Sha
  • 2,185
  • 1
  • 36
  • 61
6
votes
3 answers

Adding namespaces to C++ implementations that have a C header

We have a large project with C and C++ code. For every C++ implementation, apart from the C++ header, we usually have provide a C-header to allow functionality to be available for .c files, also. So, most of our files look like so: foo.hpp: class C…
Grim Fandango
  • 2,296
  • 1
  • 19
  • 27
6
votes
3 answers

How do I reference an external C++ namespace from within a nested one?

I have two namespaces defined in the default/"root" namespace, nsA and nsB. nsA has a sub-namespace, nsA::subA. When I try referencing a function that belongs to nsB, from inside of nsA::subA, I get an error: undefined reference to…
mph
  • 790
  • 7
  • 20
6
votes
4 answers

C# is it possible to customise/override a class keeping its class name?

If I have a class namespaceX.classA which uses namespaceX.classB is it possible to customise/overide/extend classB without breaking ClassA by using the same namespace (?) and class name? If so please could you explain how I go about doing this?…
user427165
6
votes
4 answers

Non-(X)HTML Attributes... any disadvantages?

I've generally tried to stick with DOM-only attributes when writing Javascript. Now that I've switched from Prototype to jQuery, I can get some serious mileage out of adding my own attributes to various DOM elements, mostly in the realm of being…
Don Werve
  • 5,100
  • 2
  • 26
  • 32
6
votes
2 answers

php procedural with namespace vs oop

The biggest advantage of the OOP vs procedural programming in PHP to my understanding is the sort of separation of the function names (sort of namespace). So now when we have namespace since version 5.3, what do you think - For most cases (small to…