Declaring namespaces

suggest change

A namespace declaration can look as follows:

It is recommended to only declare a single namespace per file, even though you can declare as many as you like in a single file:

namespace First {
    class A { ... }; // Define class A in the namespace First.
}

namespace Second {
    class B { ... }; // Define class B in the namespace Second.
}

namespace {
    class C { ... }; // Define class C in the root namespace.
}

Every time you declare a namespace, classes you define after that will belong to that namespace:

namespace MyProject\Shapes;

class Rectangle { ... }
class Square { ... }
class Circle { ... }

A namespace declaration can be used multiple times in different files. The example above defined three classes in the MyProject\Shapes namespace in a single file. Preferably this would be split up into three files, each starting with namespace MyProject\Shapes;. This is explained in more detail in the PSR-4 standard example.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Namespaces:
* Declaring namespaces

Table Of Contents
2 Arrays
4 Types
10 Cookies
14 JSON
15 SOAP
17 cURL
19 XML
21 Traits
22 Namespaces
35 UTF-8
36 URLs
38 PHPDoc
41 Loops
44 Closur
72 YAML
77 Cache
78 Streams
81 PDO
82 SQLite3
83 Sockets
87 MongoDB
93 IMAP
94 Redis
95 Imagick
102 APCu
108 PSR