Namespaces overview

(PHP 5 >= 5.3.0, PHP 7) Examples: Namespace syntax example PHP Namespaces provide a way in which to group related classes, interfaces, functions and constants. Here is an example of namespace syntax in PHP: <?php namespace my\name; // see "Defining Namespaces" section class MyClass {} function myfunction() {} const MYCONST = 1; $a = new MyClass; $c = new \my\name\MyClass; // see

Defining namespaces

(PHP 5 >= 5.3.0, PHP 7) Examples: Declaring a single namespace Although any valid PHP code can be contained within a namespace, only the following types of code are affected by namespaces: classes (including abstracts and traits), interfaces, functions and constants. Namespaces are declared using the namespace keyword. A file containing a namespace must declare the namespace at the to

Declaring sub-namespaces

(PHP 5 >= 5.3.0, PHP 7) Examples: Declaring a single namespace with hierarchy Much like directories and files, PHP namespaces also contain the ability to specify a hierarchy of namespace names. Thus, a namespace name can be defined with sub-levels: <?php namespace MyProject\Sub\Level; const CONNECT_OK = 1; class Connection { /* ... */ } function connect() { /* ... */  } ?>

Defining multiple namespaces in the same file

(PHP 5 >= 5.3.0, PHP 7) Examples: Declaring multiple namespaces, simple combination syntax Multiple namespaces may also be declared in the same file. There are two allowed syntaxes. <?php namespace MyProject; const CONNECT_OK = 1; class Connection { /* ... */ } function connect() { /* ... */  } namespace AnotherProject; const CONNECT_OK = 1; class Connection { /* ... */ } funct

Using namespaces: Basics

(PHP 5 >= 5.3.0, PHP 7) Examples: file1.php Here is an example of the three kinds of syntax in actual code: <?php namespace Foo\Bar\subnamespace; const FOO = 1; function foo() {} class foo {     static function staticmethod() {} } ?> file2.php <?php namespace Foo\Bar; include 'file1.php'; const FOO = 2; function foo() {} class foo {     static function staticmethod() 

Namespaces and dynamic language features

(PHP 5 >= 5.3.0, PHP 7) Examples: Dynamically accessing elements PHP's implementation of namespaces is influenced by its dynamic nature as a programming language. Thus, to convert code like the following example into namespaced code: example1.php: <?php class classname {     function __construct()     {         echo __METHOD__,"\n";     } } function funcname() {     echo __FUNCTIO

namespace keyword and __NAMESPACE__ constant

(PHP 5 >= 5.3.0, PHP 7) Examples: __NAMESPACE__ example, namespaced code PHP supports two ways of abstractly accessing elements within the current namespace, the __NAMESPACE__ magic constant, and the namespace keyword. The value of __NAMESPACE__ is a string that contains the current namespace name. In global, un-namespaced code, it contains an empty string. <?php namespace MyPro

Using namespaces: Aliasing/Importing

(PHP 5 >= 5.3.0, PHP 7) Examples: importing/aliasing with the use operator The ability to refer to an external fully qualified name with an alias, or importing, is an important feature of namespaces. This is similar to the ability of unix-based filesystems to create symbolic links to a file or to a directory. All versions of PHP that support namespaces support three kinds of aliasing

Global space

(PHP 5 >= 5.3.0, PHP 7) Examples: Using global space specification Without any namespace definition, all class and function definitions are placed into the global space - as it was in PHP before namespaces were supported. Prefixing a name with \ will specify that the name is required from the global space even in the context of the namespace. <?php namespace A\B\C; /* This functi

Using namespaces: fallback to global function/constant

(PHP 5 >= 5.3.0, PHP 7) Examples: Accessing global classes inside a namespace Inside a namespace, when PHP encounters an unqualified Name in a class name, function or constant context, it resolves these with different priorities. Class names always resolve to the current namespace name. Thus to access internal or non-namespaced user classes, one must refer to them with their fully quali