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
7
votes
1 answer

PHP How to get a method name with a class and namespace path as a string?

I really hate to write this question because I'm kind-of "research guy" and, well, I always find what I'm searching for... But this one bugs me a lot and I can't find the answer anywhere... So, here it goes: As the title says I need to get a method…
7
votes
1 answer

Best solution dealing with default namespaces in XML and XSLT

If you have a default namespace in your input XML and in the XSLT transformation to be done you only want to add one element within that namespace, how would you all do this? Do you declare the namespace also as a default namespace into the XSLT? Or…
Mark Veenstra
  • 4,691
  • 6
  • 35
  • 66
7
votes
5 answers

G++ & Clang++ - namespace std _GLIBCXX_VISIBILITY(default)

I am trying to compile my C++ code using clang++ but keep getting this error with the conflicting namespace. My main.cpp file is a simple Hello World program (for debugging). I have a feeling the issue is with my version of GCC or clang that I…
Adam
  • 510
  • 1
  • 5
  • 21
7
votes
1 answer

XML namespace of unprefixed child element where parent has xmlns:prefix declared

I'm aware of the default namespace in XML, e.g. declare xmlns="xxx" for an element, and this namespace will apply to all children without a prefix. My question is, if instead of xmlns="...", xmlns:pre="..." is declared in the element, are unprefixed…
Brett
  • 165
  • 6
7
votes
3 answers

When can I pass a function handle?

I have a function for cached evaluation. As one of the arguments, it takes a function handle. Under some circumstances, the function handle is unaccessible, and I don't quite understand why. The example below shows what got me stumped: >> A.a =…
gerrit
  • 24,025
  • 17
  • 97
  • 170
7
votes
3 answers

using C++ with namespace in C

Using Eclpse on Linux I have defined a C++ class, named ABC (ABC.hpp): #ifndef ABC_HPP_ #define ABC_HPP_ #include // namespace my { <---- COMMENTED OUT class ABC { private: int m_number; public: ABC(int p_number); …
Kilátó
  • 403
  • 3
  • 10
  • 19
7
votes
5 answers

What are the guidelines for avoiding namespace and type name conflicts in C#?

Clean code should use short, meaningful names for entities. Thus, given an application that deals with, say, robots, I would want to have a class library project Robot with namespace Robot that has a class Robot. Lets assume that there is only a…
mrts
  • 16,697
  • 8
  • 89
  • 72
7
votes
2 answers

xmlns namespace breaking lxml

I am trying to open an xml file, and get values from certain tags. I have done this a lot but this particular xml is giving me some issues. Here is a section of the xml file:
speedyrazor
  • 3,127
  • 7
  • 33
  • 51
7
votes
1 answer

PHP trying to use autoload function to find PDO class

This has been bugging me for some time now and I can't seem to make sense of it. My phpinfo reports that PDO is installed and I can connect to my database on my index.php file. But when I try to open a PDO connection on a namespaced class, php is…
user2627854
  • 73
  • 1
  • 3
7
votes
1 answer

Javascript namespacing convention

wI'm unsure which is the better namespace convention to use. var App = {}; // global variable, the root of our namespace (function() { App.something = function() { } })(); or (function() { window.App = {}; //global variable, the root…
monsoon
  • 97
  • 5
7
votes
3 answers

How to display XSD validated XML using XSLT

I've been fighting with this for some time now, and haven't been able to find a clear answer to this yet. As I understand correctly I can store data in an XML file, validate it using an XSD and then display the data neatly using an XSLT. However…
Robert Sirre
  • 672
  • 1
  • 8
  • 28
7
votes
3 answers

Production uninitialized constant custom class stored in lib (heroku)

I have a custom class stored in /lib (/lib/buffer_app.rb): require 'HTTParty' class BufferApp include HTTParty base_uri 'https://api.bufferapp.com/1' def initialize(token, id) @token = token @id = id end def create(text) …
Jayson Lane
  • 2,828
  • 1
  • 24
  • 39
7
votes
1 answer

How to tell Haskell to not import the same instance from two modules?

I'm using the following typeclass: module T where class T a where v :: a An instance of T Int that I implemented: import T import A (av) instance T Int where v = 0 main = putStrLn (av ++ show v) And a module that I want to use a value from,…
Dog
  • 7,707
  • 8
  • 40
  • 74
7
votes
3 answers

How to add namespace while signing XML file using javax.xml.crypto.dsig.*?

I'm trying to sign an xml file using enveloped signature and javax.xml.crypto.dsig.* classes. As a result I get file with correct Signature content but with no namespace defined. How can I add xmlns:ds="http://www.w3.org/2000/09/xmldsig#" namespace…
jasiustasiu
  • 1,468
  • 2
  • 20
  • 28
7
votes
2 answers

Is a class with only static methods preferable to a namespace?

I was inspired by the comments under this question. I didn't see any reason why a class with only static functions would be a better design than a namespace (with just functions). Any list of pros and cons of these two approaches are welcomed. It…
GuLearn
  • 1,814
  • 2
  • 18
  • 32