Namespaces provide a way to avoid naming conflicts and to group namespace members into a hierarchy. Any code may be contained within a namespace, but only four code constructs are affected: classes, interfaces, functions and constants.

Creating namespaces

A construct that is not included in a namespace belongs to the global namespace.

// Global code/namespaceclass MyClass {}

To assign the construct to another namespace a namespace directive is defined. Any code constructs below the namespace directive will belong to that namespace. The naming convention for namespaces is all lowercase.

namespace my; // Belongs to my namespace ...

Get PHP Quick Scripting Reference now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.