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

Why doesn't a using directive affect ADL?

I am trying to understand why the following code does not compile: namespace ns { struct S {}; } namespace alleq { inline bool operator==(const ns::S &, const ns::S &) { return true; } } namespace ns { using namespace alleq; //…
user3188445
  • 4,062
  • 16
  • 26
6
votes
3 answers

Implementing namespaces in fatfree framework

I am trying to use namespaces in fatfree framework, but somehow its not able to find the class following is my setup routes.ini [routes] GET /=Src\Controllers\Index->index index.php namespace Src\Controllers; class Index { function index($f3)…
Ketan Mujumdar
  • 113
  • 2
  • 9
6
votes
2 answers

Is there a technical reason for the "struct namespace" in C?

In C, most of the code declaring structs will follow this pattern: /* struct forward-declaration */ typedef struct T T ; /* struct definition */ typedef struct T { /* etc. */ } T ; This is so prevalent most developers I talked with didn't even…
paercebal
  • 81,378
  • 38
  • 130
  • 159
6
votes
1 answer

Why does CppCheck give an array access out of bounds error for this static const array?

CppCheck 1.67 has identified and array accessed out of bounds error on one of my projects. I didn't think the code was wrong, so I have stripped down the code to the bare minimum example that still raises the same error. Why does CppCheck give the…
6
votes
2 answers

No member named 'name' in namespace 'namespace'

I can't for the life of me figure out why this error is being generated as I'm pretty sure the syntax is correct (obviously I'm wrong!). So I figured I'd see if anyone here could point it out for me. main.cpp #include "Object.h" int main(){ out…
Mmm Donuts
  • 9,551
  • 6
  • 27
  • 49
6
votes
1 answer

How to import static function with namespace in php?

class A declared at namespace1. namesapce namesapce1; class A { public static function fun1() { } } I want to use fun1() inside class B: namespace namesapce2; use ???? as fun1 class B { public static func2() { fun1(); …
david
  • 161
  • 3
  • 9
6
votes
3 answers

How to define using statements in web.config?

I'm using MySql in my asp.net project. But I don't want to type every "using MySql.Data.MySqlClient;" statement in every aspx.cs/aspx.vb file. How can I define this lines in web.config file? I've defined some namespaces like below but this only…
HasanG
  • 12,734
  • 29
  • 100
  • 154
6
votes
3 answers

Should it be in a namespace?

Do I have to put code from .cpp in a namespace from corresponding .h or it's enough to just write using declaration? //file .h namespace a { /*interface*/ class my { }; } //file .cpp using a::my; // Can I just write in this file this declaration…
There is nothing we can do
  • 23,727
  • 30
  • 106
  • 194
6
votes
5 answers

XmlDocument type not found even though I've referenced System.XML?

I've referenced System.Xml: using System.Xml; Then in this line: XmlDocument xdoc = new XmlDocument(); I get: The type or namespace name 'XmlDocument' could not be found What could there possibly be wrong ? Info: .NET 3.5, C#, triple checked…
Marcelo
  • 3,371
  • 10
  • 45
  • 76
6
votes
4 answers

How can I get JDOM/XPath to ignore namespaces?

I need to process an XML DOM, preferably with JDOM, where I can do XPath search on nodes. I know the node names or paths, but I want to ignore namespaces completely because sometimes the document comes with namespaces, sometimes without, and I can't…
AdSR
  • 2,097
  • 3
  • 15
  • 9
6
votes
1 answer

How to fix namespace not being determined in new folders project?

I upgraded from phpstorm7 to phpstorm8. Now, whenever I create a new PHP class in an existing folder, the namespace is inserted correctly. Yet when I create an empty folder and create in there a new class, the namespace is empty and I would have to…
k0pernikus
  • 60,309
  • 67
  • 216
  • 347
6
votes
2 answers

Importing a local variable in a function into timeit

I need to time the execution of a function across variable amounts of data. def foo(raw_data): preprocessed_data = preprocess_data(raw_data) time = timeit.Timer('module.expensive_func(preprocessed_data)', 'import module').timeit() However,…
EMiller
  • 2,792
  • 4
  • 34
  • 55
6
votes
2 answers

Autoload with composer of classes inside a single file

I'm trying to use a library that uses namespaces but has a part of the code auto-generated so they generate more than one class in a single file. We use composer and I tried to add the namespace define in psr-4 like this "name\space\prefix\":…
Khriz
  • 5,888
  • 6
  • 34
  • 39
6
votes
1 answer

Is it possible to autoload a file based on the namespace in PHP?

Would what mentioned in the title be possible? Python module style that is. See this example for what I exactly mean. index.php
izym
  • 143
  • 5
6
votes
2 answers

Resolving namespace conflicts

I've got a namespace with a ton of symbols I use, but I want to overwrite one of them: external_library.h namespace LottaStuff { class LotsOfClasses {}; class OneMoreClass {}; }; my_file.h using namespace LottaStuff; namespace…
Kyle
  • 4,487
  • 3
  • 29
  • 45
1 2 3
99
100