README.namespaces 5.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
Design
======

Main assumption of the model is that the problem that we are to solve is the
problem of the very long class names in PHP libraries. We would not attempt
to take autoloader's job or create packaging model - only make names
manageable.

Namespaces are defined the following way:

Zend/DB/Connection.php:
<?php
13
namespace Zend\DB;
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

class Connection {
}

function connect() {
}
?>

Namespace definition does the following:
All class and function names inside are automatically prefixed with
namespace name. Inside namespace, local name always takes precedence over
global name. Several files may be using the same namespace.
The namespace declaration statement must be the very first statement in
the file. The only exception is "declare" statement that can be used before.

Every class and function in a namespace can be referred to by the full name
30
- e.g. Zend\DB\Connection or Zend\DB\connect - at any time.
31 32 33

<?php
require 'Zend/Db/Connection.php';
34 35
$x = new Zend\DB\Connection;
Zend\DB\connect();
36 37 38 39 40 41
?>

Namespace or class name can be imported:

<?php
require 'Zend/Db/Connection.php';
42 43
use Zend\DB;
use Zend\DB\Connection as DbConnection;
44

45 46
$x = new Zend\DB\Connection();
$y = new DB\connection();
47
$z = new DbConnection();
48
DB\connect();
49 50 51
?>

The use statement only defines name aliasing. It may create name alias for
52 53
namespace or class. The simple form of statement "use A\B\C\D;" is
equivalent to "use A\B\C\D as D;". The use statement can be used at any
54 55 56 57 58
time in the global scope (not inside function/class) and takes effect from 
the point of definition down to the end of file. It is recommended however to
place the use statements at the beginning of the file. The use statements have
effect only on the file where they appear.

59 60
The special "empty" namespace (\ prefix) is useful as explicit global
namespace qualification. All class and function names started from \
61 62 63
interpreted as global.

<?php 
64
namespace A\B\C;
65

66
$con = \mysql_connect(...);
67 68 69 70 71 72
?>

A special constant __NAMESPACE__ contains the name of the current namespace. 
It can be used to construct fully-qualified names to pass them as callbacks.

<?php
73
namespace A\B\C;
74 75 76 77

function foo() {
}

78
set_error_handler(__NAMESPACE__ . "\foo");
79 80 81 82 83 84 85
?>

In global namespace __NAMESPACE__ constant has the value of empty string.

Names inside namespace are resolved according to the following rules:

1) all qualified names are translated during compilation according to
86 87
current import rules. So if we have "use A\B\C" and then "C\D\e()"
it is translated to "A\B\C\D\e()".
88
2) unqualified class names translated during compilation according to
89 90
current import rules. So if we have "use A\B\C" and then "new C()" it
is translated to "new A\B\C()".
91 92 93 94 95
3) inside namespace, calls to unqualified functions that are defined in 
current namespace (and are known at the time the call is parsed) are 
interpreted as calls to these namespace functions.
4) inside namespace, calls to unqualified functions that are not defined 
in current namespace are resolved at run-time. The call to function foo() 
96 97
inside namespace (A\B) first tries to find and call function from current 
namespace A\B\foo() and if it doesn't exist PHP tries to call internal
98
function foo(). Note that using foo() inside namespace you can call only 
99
internal PHP functions, however using \foo() you are able to call any
100 101 102 103 104
function from the global namespace.
5) unqualified class names are resolved at run-time. E.q. "new Exception()"
first tries to use (and autoload) class from current namespace and in case 
of failure uses internal PHP class. Note that using "new A" in namespace 
you can only create class from this namespace or internal PHP class, however
105
using "new \A" you are able to create any class from the global namespace.
106
6) Calls to qualified functions are resolved at run-time. Call to
107 108
A\B\foo() first tries to call function foo() from namespace A\B, then
it tries to find class A\B (__autoload() it if necessary) and call its
109 110
static method foo()
7) qualified class names are interpreted as class from corresponding
111
namespace. So "new A\B\C()" refers to class C from namespace A\B.
112 113 114 115 116 117 118

Examples
--------
<?php
namespace A;
foo();   // first tries to call "foo" defined in namespace "A"
         // then calls internal function "foo"
119
\foo(); // calls function "foo" defined in global scope
120 121 122 123 124 125
?>

<?php
namespace A;
new B();   // first tries to create object of class "B" defined in namespace "A"
           // then creates object of internal class "B"
126
new \B(); // creates object of class "B" defined in global scope
127 128 129 130
?>

<?php
namespace A;
131
new A(); // first tries to create object of class "A" from namespace "A" (A\A)
132 133 134 135 136
         // then creates object of internal class "A"
?>

<?php
namespace A;
137
B\foo();   // first tries to call function "foo" from namespace "A\B"
138
            // then calls method "foo" of internal class "B"
139
\B\foo(); // first tries to call function "foo" from namespace "B"
140 141 142 143 144 145
            // then calls method "foo" of class "B" from global scope
?>

The worst case if class name conflicts with namespace name
<?php
namespace A;
146
A\foo();   // first tries to call function "foo" from namespace "A\A"
147 148 149
            // then tries to call method "foo" of class "A" from namespace "A"
            // then tries to call function "foo" from namespace "A"
            // then calls method "foo" of internal class "A"
150
\A\foo(); // first tries to call function "foo" from namespace "A"
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
            // then calls method "foo" of class "A" from global scope
?>

TODO
====

* Support for namespace constants?

* performance problems
  - calls to internal functions in namespaces are slower, because PHP first
    looks for such function in current namespace
  - calls to static methods are slower, because PHP first tries to look
    for corresponding function in namespace

* Extend the Reflection API?
  * Add ReflectionNamespace class
    + getName()
    + getClasses()
    + getFunctions()
    + getFiles()
  * Add getNamespace() methods to ReflectionClass and ReflectionFunction

* Rename namespaces to packages?