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

`using` declaration for a user-defined literal operator

Is it possible to have a using declaration for the literals operator, operator ""? E.g., #include namespace MyNamespace { constexpr std::chrono::hours operator "" _hr(unsigned long long n){ return std::chrono::hours{n}; } // ...…
Kyle Strand
  • 15,941
  • 8
  • 72
  • 167
6
votes
1 answer

ClojureScript split one namespace into multiple files

I've read this thread, but it seems like there are no load and load-file in ClojureScript. Is it possible to separate a single namespace over multiple files? The reason I want to do that is because I'm using Om and I want to separate components into…
Victor Marchuk
  • 13,045
  • 12
  • 43
  • 67
6
votes
2 answers

Where should I write my code so that Composer can autoload my PHP classes?

I'm new to Composer, namespaces, and autoload and I wasn't able to figure out where to write my code (under vendor?). I have created a directory named ilhan under the vendor, and a file named People.php. Then in the main index.php file using use…
ilhan
  • 8,700
  • 35
  • 117
  • 201
6
votes
1 answer

How do I find what reference file a namespace in a using directive is coming from?

I have some older software that I am maintaining. I am attempting to find out what specific reference file contains the namespace that is being brought into the project by the using directive. Is there a simple way of finding which reference file…
Trevallion
  • 81
  • 1
  • 1
  • 7
6
votes
1 answer

Python class and global vs local variables

I have a problem understanding what is happening with the outcome of the following pieces of code: my_str = "outside func" def func(): my_str = "inside func" class C(): print(my_str) print((lambda:my_str)()) my_str =…
Dargor
  • 623
  • 1
  • 4
  • 12
6
votes
1 answer

Namespace prefix NS1 for href on %tagElement% is not defined, setAttributeNS

I got this error in safari while trying to convert svg to base64 url via code: $svgCopy = $('svg').clone() html = $('
').append($svgCopy).html() imgSrc = 'data:image/svg+xml;base64,' + btoa(html) imgEl.src = imgSrc The problem is that when you…
extempl
  • 2,987
  • 1
  • 26
  • 38
6
votes
2 answers

SimpleXML SOAP response Namespace issues

After spending SEVERAL frustrated hours on this I am asking for your help. I am trying to get the content of particular nodes from a SOAP response. The response is
Stu
  • 63
  • 1
  • 1
  • 3
6
votes
1 answer

Translating Perl to Ruby - modules vs. classes

Namespacing in Perl is pretty straight forward, but I can't seem to find a solution for translating this very simple Perl class hierarchy to Ruby. Perl lib/Foo.pm package Foo; use Foo::Bar; sub bar { return…
daskraken
  • 83
  • 4
6
votes
2 answers

ASMX Web Service - "This web service is using http://tempuri.org/ as its default namespace." message - but it shouldn't be

I've created a web service using Visual Studio ( 2005 - I know I'm old school ) and it all compiles fine but when it opens I get warned thus: This web service does not conform to WS-I Basic Profile v1.1. And furthermore: This web service is using…
glenatron
  • 11,018
  • 13
  • 64
  • 112
6
votes
1 answer

How to use namespaced function with dplyr::mutate_each?

I am trying to use dplyr::mutate_each with some external functions without attaching actual libraries dplyr::tbl_df(iris) %>% dplyr::mutate_each(dplyr::funs(stringi::stri_trim_both)) but it fails with following error: Error: unsupported type…
zero323
  • 322,348
  • 103
  • 959
  • 935
6
votes
1 answer

XSLT to rename qualified root element, keep other namespaces

I am trying to write an XSLT to manipulate our XML where we need to: Rename the qualified root element from 'tool:view' to 'indexes' Rename 'tool:view' attribute from 'name' to 'view' Change default namespace from…
Jason Morse
  • 6,204
  • 5
  • 29
  • 29
6
votes
1 answer

Creating namespace prefixed XML nodes in Java DOM

I am creating several XML files via Java and up to this point everything worked fine, but now I've run into a problem when trying to create a file with namespace prefixed nodes, i.e, stuff like ... using a refactored version…
daZza
  • 1,669
  • 1
  • 29
  • 51
6
votes
1 answer

Weird compatibility problem with .Net 3.5 and 4.0 assemblies (NATUPnPLib)

I'm having trouble getting NATUPnP 1.0 Type Library to work with Framework 3.5 in Visual Studio 2010. If I use .Net 4.0, it works just fine, but with .Net 3.5, NATUPNPLib's namespace looks excactly like NETCONLib's. For example this Port Forwarding…
Juha
  • 672
  • 7
  • 20
6
votes
1 answer

Why do both "std::printf" and "printf" compile when using rather than in C++?

To my knowledge, headers of the form cxyz are identical to xyz.h with the only difference being that cxyz places all of the contents of xyz.h under the namespace std. Why is it that the following programs both compile on GCC 4.9 and clang…
ra1nmaster
  • 662
  • 1
  • 8
  • 21
6
votes
3 answers

Can I do "using namespace.class"?

I was wondering this as I apparently cannot do this. I have added my .dll as as reference and have added using myNamespace; but whenever I want to call one of the functions, I have to use myClass.myMethod(); is there anything I can do so I wont…
ohSkittle
  • 149
  • 3
  • 14