Skip to content

Commit 58f196b

Browse files
committed
- Changed namespace separator
1 parent 6b926eb commit 58f196b

File tree

1 file changed

+35
-35
lines changed

1 file changed

+35
-35
lines changed

Diff for: README.namespaces

+35-35
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Namespaces are defined the following way:
1010

1111
Zend/DB/Connection.php:
1212
<?php
13-
namespace Zend::DB;
13+
namespace Zend\DB;
1414

1515
class Connection {
1616
}
@@ -27,127 +27,127 @@ The namespace declaration statement must be the very first statement in
2727
the file. The only exception is "declare" statement that can be used before.
2828

2929
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.
30+
- e.g. Zend\DB\Connection or Zend\DB\connect - at any time.
3131

3232
<?php
3333
require 'Zend/Db/Connection.php';
34-
$x = new Zend::DB::Connection;
35-
Zend::DB::connect();
34+
$x = new Zend\DB\Connection;
35+
Zend\DB\connect();
3636
?>
3737

3838
Namespace or class name can be imported:
3939

4040
<?php
4141
require 'Zend/Db/Connection.php';
42-
use Zend::DB;
43-
use Zend::DB::Connection as DbConnection;
42+
use Zend\DB;
43+
use Zend\DB\Connection as DbConnection;
4444

45-
$x = new Zend::DB::Connection();
46-
$y = new DB::connection();
45+
$x = new Zend\DB\Connection();
46+
$y = new DB\connection();
4747
$z = new DbConnection();
48-
DB::connect();
48+
DB\connect();
4949
?>
5050

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

59-
The special "empty" namespace (:: prefix) is useful as explicit global
60-
namespace qualification. All class and function names started from ::
59+
The special "empty" namespace (\ prefix) is useful as explicit global
60+
namespace qualification. All class and function names started from \
6161
interpreted as global.
6262

6363
<?php
64-
namespace A::B::C;
64+
namespace A\B\C;
6565

66-
$con = ::mysql_connect(...);
66+
$con = \mysql_connect(...);
6767
?>
6868

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

7272
<?php
73-
namespace A::B::C;
73+
namespace A\B\C;
7474

7575
function foo() {
7676
}
7777

78-
set_error_handler(__NAMESPACE__ . "::foo");
78+
set_error_handler(__NAMESPACE__ . "\foo");
7979
?>
8080

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

8383
Names inside namespace are resolved according to the following rules:
8484

8585
1) all qualified names are translated during compilation according to
86-
current import rules. So if we have "use A::B::C" and then "C::D::e()"
87-
it is translated to "A::B::C::D::e()".
86+
current import rules. So if we have "use A\B\C" and then "C\D\e()"
87+
it is translated to "A\B\C\D\e()".
8888
2) unqualified class names translated during compilation according to
89-
current import rules. So if we have "use A::B::C" and then "new C()" it
90-
is translated to "new A::B::C()".
89+
current import rules. So if we have "use A\B\C" and then "new C()" it
90+
is translated to "new A\B\C()".
9191
3) inside namespace, calls to unqualified functions that are defined in
9292
current namespace (and are known at the time the call is parsed) are
9393
interpreted as calls to these namespace functions.
9494
4) inside namespace, calls to unqualified functions that are not defined
9595
in current namespace are resolved at run-time. The call to function foo()
96-
inside namespace (A::B) first tries to find and call function from current
97-
namespace A::B::foo() and if it doesn't exist PHP tries to call internal
96+
inside namespace (A\B) first tries to find and call function from current
97+
namespace A\B\foo() and if it doesn't exist PHP tries to call internal
9898
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
99+
internal PHP functions, however using \foo() you are able to call any
100100
function from the global namespace.
101101
5) unqualified class names are resolved at run-time. E.q. "new Exception()"
102102
first tries to use (and autoload) class from current namespace and in case
103103
of failure uses internal PHP class. Note that using "new A" in namespace
104104
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.
105+
using "new \A" you are able to create any class from the global namespace.
106106
6) Calls to qualified functions are resolved at run-time. Call to
107-
A::B::foo() first tries to call function foo() from namespace A::B, then
108-
it tries to find class A::B (__autoload() it if necessary) and call its
107+
A\B\foo() first tries to call function foo() from namespace A\B, then
108+
it tries to find class A\B (__autoload() it if necessary) and call its
109109
static method foo()
110110
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.
111+
namespace. So "new A\B\C()" refers to class C from namespace A\B.
112112

113113
Examples
114114
--------
115115
<?php
116116
namespace A;
117117
foo(); // first tries to call "foo" defined in namespace "A"
118118
// then calls internal function "foo"
119-
::foo(); // calls function "foo" defined in global scope
119+
\foo(); // calls function "foo" defined in global scope
120120
?>
121121

122122
<?php
123123
namespace A;
124124
new B(); // first tries to create object of class "B" defined in namespace "A"
125125
// then creates object of internal class "B"
126-
new ::B(); // creates object of class "B" defined in global scope
126+
new \B(); // creates object of class "B" defined in global scope
127127
?>
128128

129129
<?php
130130
namespace A;
131-
new A(); // first tries to create object of class "A" from namespace "A" (A::A)
131+
new A(); // first tries to create object of class "A" from namespace "A" (A\A)
132132
// then creates object of internal class "A"
133133
?>
134134

135135
<?php
136136
namespace A;
137-
B::foo(); // first tries to call function "foo" from namespace "A::B"
137+
B\foo(); // first tries to call function "foo" from namespace "A\B"
138138
// then calls method "foo" of internal class "B"
139-
::B::foo(); // first tries to call function "foo" from namespace "B"
139+
\B\foo(); // first tries to call function "foo" from namespace "B"
140140
// then calls method "foo" of class "B" from global scope
141141
?>
142142

143143
The worst case if class name conflicts with namespace name
144144
<?php
145145
namespace A;
146-
A::foo(); // first tries to call function "foo" from namespace "A::A"
146+
A\foo(); // first tries to call function "foo" from namespace "A\A"
147147
// then tries to call method "foo" of class "A" from namespace "A"
148148
// then tries to call function "foo" from namespace "A"
149149
// then calls method "foo" of internal class "A"
150-
::A::foo(); // first tries to call function "foo" from namespace "A"
150+
\A\foo(); // first tries to call function "foo" from namespace "A"
151151
// then calls method "foo" of class "A" from global scope
152152
?>
153153

0 commit comments

Comments
 (0)