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

Assigning namespaces in C++

I have a little confusion regarding namespaces. Here is what I know. Normally if you have namespaces such as this in the code namespace foo { namespace gfoo { class apple {..}; } } Now by using the following code using…
MistyD
  • 16,373
  • 40
  • 138
  • 240
7
votes
1 answer

UUID namespace and name. How to retrieve it?

I have the following problem. I assign to any object in my database a UUID. When I get a UUID from any source, to obtain the object I need to know what type of object is, so I can query the correct table in the database. As far as I understand, UUID…
Stefano Borini
  • 138,652
  • 96
  • 297
  • 431
7
votes
3 answers

What does "shadows it in the MATLAB path" mean? How to do it in a file?

I need to do this always before running unitTester file. I cannot understand why this is required. What does this mean? And why is the "Add to Path > Selected Folders and Subfolders" not enough? [Update] This here may be the problem. The kernel is…
hhh
  • 50,788
  • 62
  • 179
  • 282
7
votes
1 answer

Can I avoid using the class name in the .cpp file if I declare a namespace in the header?

In C++, all I want to do is declare a DisplayInfo class in a .h file, and then in the .cpp file, not have to type the first DisplayInfo::DisplayInfo() and every function definition. Sadly, I've looked at over 20 topics and my C++ book for over two…
7
votes
6 answers

How come classes in subfolders in my App_Code folder are not being found correctly?

I am getting the following error when I put class files in subfolders of my App_Code folder: errorCS0246: The type or namespace name 'MyClassName' could not be found (are you missing a using directive or an assembly reference?) This class is not in…
Greg
  • 7,233
  • 12
  • 42
  • 53
7
votes
2 answers

Namespace vs Class Declaration

I'm new to C# and I can't seem to find any info on this so I will ask it here. Do classes in namespaces have to ever be declared? using System; public class myprogram { void main() { // The console class does not have to be…
Ralph
  • 889
  • 14
  • 25
7
votes
1 answer

How narrow should a using declaration be?

I have this little class widget that uses a std::string. It uses it in many places, often in conjunction to a std::vector. So you can see, that the typenames become very long and annoying. I want to utilize the using keyword, i.e. using…
Xlaudius
  • 1,757
  • 2
  • 13
  • 16
7
votes
2 answers

Laravel 4 target interface is not instantiable

This is related to this question How to register a namespace in laravel 4 but I believe I got that worked out and namespaces are working now. There is a new problem I've run into. I believe the error is coming from trying to type hint in the…
isimmons
  • 2,016
  • 2
  • 20
  • 32
7
votes
3 answers

python namespace: __main__.Class not isinstance of package.Class

Consider you have two python files as defined below. Say one is a general package (class2), and the other one does specific overrides and serves as the executable (class1). class1.py: #!/usr/bin/python class Test(object): pass class…
Lars Hanke
  • 614
  • 5
  • 16
7
votes
2 answers

Fatal Error RecursiveIteratorIterator not found

As the title says, when I instantiate a class I get this message : Fatal error: Class 'Envato\RecursiveIteratorIterator' not found in C:\Users\rgr\Apache\htdocs\Roland Groza [ 3.0 ]\class\envato\envato.php on line 359 You can view the class here :…
Roland
  • 9,321
  • 17
  • 79
  • 135
7
votes
5 answers

C++ Name Resolution

I'm wondering a bit about the namespace and using in C++ basically I would like to know the differences and figure out how to use it in the best way. As I see it there are (at least) three ways to resolve a class name and I am not sure how to choose…
qrikko
  • 2,483
  • 2
  • 22
  • 35
7
votes
1 answer

C++ unnamed(anonymous) namespace definition

C++03 Standard 7.3.1.1 [namespace.unnamed] paragraph 1: (and C++11 Standard also use similar definition) An unnamed-namespace-definition behaves as if it were replaced by namespace unique { /* empty body */ } using namespace unique; namespace…
yohjp
  • 2,985
  • 3
  • 30
  • 100
7
votes
1 answer

not catching PDOException in namespace

I can't know how can i catch PDOException in the follow code,please tell me where throw exception in the follow code? i have (directory) : - folder -1) b.php -2) c.php - autoloader in the b.php :
navid
  • 823
  • 2
  • 11
  • 29
7
votes
2 answers

How to define which variables or functions from a package are exported

My R package uses an internal variable x. If I load the package (I've only tried using devtools::load_all), then x doesn't appear in the ls() list, but it does have a value. How can I avoid this? I'm fine with the user being able to access the…
Andreas
  • 7,470
  • 10
  • 51
  • 73
7
votes
1 answer

What happens when a namespace and class share a name in PHP?

When using the pseudo namespacing pattern of PEAR and Zend, it is common to come across class heirarchies that look like this: Zend/ Db.php Db/ Expr.php Where DB.php contains a class named Zend_Db and Expr.php contains a class named…
Daniel Bingham
  • 12,414
  • 18
  • 67
  • 93