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

Is JQuery namespace a good practice?

Can anyone explain to me please that if using a namespace is a good coding practice. And why is it needed? If it was a good approach why JQuery didn't include it by default. There is a separate plugin to allow this functionality. I saw this post…
Amir
  • 349
  • 2
  • 4
  • 13
6
votes
1 answer

Is A::B::B::B::B...B::f() right ? Why could i do that?

I don't know how to explain this: namespace A { struct B { static void f() { } }; } int main() { A::B::B::B::B::B::B::B::B::B::B::f(); } Why could i do : A::B::B::B::B::B::B::B::B::B::B::f(); I don't understand it why it's happening.
Ghasem Ramezani
  • 2,683
  • 1
  • 13
  • 32
6
votes
2 answers

How do I copy all resources to a new namespace?

I would like to create a new namespace which is identical to the old one. My approach would look something like this (full command below): kubectl get -o json --namespace OLD-NAMESPACE | jq '.items[].metadata.namespace = "NEW-NAMESPACE"'…
User12547645
  • 6,955
  • 3
  • 38
  • 69
6
votes
1 answer

How to load-string to current namespace (not core)?

I can load arbitrary Clojure source using: (load-string source) However, if namespace wasn't provided, it loads code to clojure.core namespace. For example, following code: (load-string "(defn add [a b] (+ a b))") defines a…
Goran Jovic
  • 9,418
  • 3
  • 43
  • 75
6
votes
5 answers

C#: How to get the name (with prefix) from XElement as string?

This might be duplicate since my question seems so trivial, but I haven't been able to find the answer here on stackoverflow.com. I have an XElement with data like this: My value Question: How do I get the complete…
Chau
  • 5,540
  • 9
  • 65
  • 95
6
votes
2 answers

Alias to call model class in artisan tinker

This question might sound silly, but I really wish to know if there is a way to solve the problem of long model path in artisan commands. I have a models organized in the following structure: `app\Models\Auth\User.php` And in the User model, I…
Lamar
  • 1,761
  • 4
  • 24
  • 50
6
votes
3 answers

How can I get Axis 1.4 to not generate several prefixes for the same XML namespace?

I am receiving SOAP requests from a client that uses the Axis 1.4 libraries. The requests have the following form:
Christian Berg
  • 14,246
  • 9
  • 39
  • 44
6
votes
3 answers

Is there a way to not use an item from a namespace?

I've learned that typing using namespace std; at the beginning of a program is a bad habit, because it includes every function in the namespace. This risks causing errors if there is a name collision. My question is, does there exist a way to…
Telescope
  • 2,068
  • 1
  • 5
  • 22
6
votes
1 answer

No access to "ldTweedie" function from mgcv when called from within another package

I am using mgcv package within my own package and so far everything worked nicely. However, I tried to introduce Tweedie family objects ("Tweedie()", "tw()") to my package, but I am getting an error when trying to use the latter. The function is as…
6
votes
2 answers

XML Comments -- How (or where) do you create XML comments for your namespaces and library?

I understand that if you /// above a class, field, method, or property Visual Studio will start establishing XML-style comments for you. However, where can I go to add XML comments for my namespaces and/or library... For example: .NET Framework…
michael
  • 14,844
  • 28
  • 89
  • 177
6
votes
2 answers

In PHP, how can I wrap procedural code in a class?

I have a large chunk of legacy php code that I need to interface with that looks like this: //legacy.php function foo() { } function bar() { } I want to be able to wrap these legacy functions in a class or somehow require_once without polluting…
james
  • 3,543
  • 8
  • 31
  • 39
6
votes
0 answers

Is there a SecurityCenter2 namespace equivalent on Windows Server 2019?

I am writing a C# application that, among other things, gathers status information about Windows Defender. On Windows 10, using ManagementObjectSearcher(@"Root\SecurityCenter2", "SELECT * FROM AntiVirusProduct") works fine to get some initial…
kgcode
  • 159
  • 3
  • 14
6
votes
1 answer

WPF - Is used anymore?

Basically, I remember that there was a time when you could use the following: (It doesn't seem to work anymore -- Deprecated perhaps?) I know I can map my XML namespaces like…
myermian
  • 31,823
  • 24
  • 123
  • 215
6
votes
4 answers

What is a namespace and how is it implemented in PHP?

I've heard the latest PHP has support for namespaces. I know variables defined in the global scope have no namespace, so how does one make a variable in a different namespace? Is it just a way of categorising variables/functions?
alex
  • 479,566
  • 201
  • 878
  • 984
6
votes
4 answers

Simple example of JavaScript namespaces, classes and inheritance

I've been asked to port some of our PHP code across to JavaScript, so that more of our logic runs client-side. What'd I'd like is a simple example that shows: a namespace ("Package") containing two classes ("Master" and "Slave") the "Master" class…
wpearse
  • 2,422
  • 2
  • 29
  • 30