PHP's namespace (namespace) is only available after php5.3. This concept is already very early in C #, and the namespace in PHP is actually the same as the C # concept.
Why should I use namespace in PHP?
Assuming that you do not use namespace, the name of each class in a project must be fixed. Because PHP has a file that corresponds to a class name, whether it's calling AutoLoad or calling a loaded class at new. So when there is no namespace, we think of various naming conventions to distinguish different classes, such as Project1_school1_class1_student or project2_school_class_student.
The introduction of namespace can be effectively circumvented, a namespace equivalent to a file path, when looking for this class, it will go to the corresponding file path to find the class definition file.
Definition and use of namespace
Defined:
123 |
<?php namespace Myproject; |
Or
12345 |
<?php namespace Myproject { } |
Use:
123 |
<?php use Myproject/School; |
123 |
<?php use Myproject/School as School1; // 别名 |
Namespaces are parsed at run time. Use is equivalent to a declaration that does not parse and load. For example, the following:
test.php
12345 |
<?php use my\name; require_once ( "/home/yejianfeng/handcode/test/namespace1.php" ); $a = new my\name\A(); $a ->Print1(); |
namespace1.php
1234567 |
<?php namespace my\name; class a { public function print1 () { echo 11; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; } } |
Although the require_once is under use, it can be run normally, because the program only loads the namespace when new My\name\a () is loaded my\name
Global classes and Namespace classes
If you want to new a global class using new \a ()
If you want to new a namespace class, use new My\namespace\a ()
The Order of namespaces
The most error-prone thing to do since you have a namespace is what the search path for this class is when you use the class.
If we can figure out this example in manual, we can all find out the order.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
<?php
namespace
A;
use
B\D, C\E
as
F;
// 函数调用 foo();
// 首先尝试调用定义在命名空间"A"中的函数foo()
// 再尝试调用全局函数 "foo"
\foo();
// 调用全局空间函数 "foo"
my\foo();
// 调用定义在命名空间"A\my"中函数 "foo"
F();
// 首先尝试调用定义在命名空间"A"中的函数 "F"
// 再尝试调用全局函数 "F"
// 类引用
new
B();
// 创建命名空间 "A" 中定义的类 "B" 的一个对象
// 如果未找到,则尝试自动装载类 "A\B"
new
D();
// 使用导入规则,创建命名空间 "B" 中定义的类 "D" 的一个对象
// 如果未找到,则尝试自动装载类 "B\D" new
F();
// 使用导入规则,创建命名空间 "C" 中定义的类 "E" 的一个对象
// 如果未找到,则尝试自动装载类 "C\E"
new
\B();
// 创建定义在全局空间中的类 "B" 的一个对象
// 如果未发现,则尝试自动装载类 "B"
new
\D();
// 创建定义在全局空间中的类 "D" 的一个对象
// 如果未发现,则尝试自动装载类 "D"
new
\F();
// 创建定义在全局空间中的类 "F" 的一个对象
// 如果未发现,则尝试自动装载类 "F"
// 调用另一个命名空间中的静态方法或命名空间函数
B\foo();
// 调用命名空间 "A\B" 中函数 "foo" B::foo();
// 调用命名空间 "A" 中定义的类 "B" 的 "foo" 方法
// 如果未找到类 "A\B" ,则尝试自动装载类 "A\B"
D::foo();
// 使用导入规则,调用命名空间 "B" 中定义的类 "D" 的 "foo" 方法
// 如果类 "B\D" 未找到,则尝试自动装载类 "B\D"
\B\foo();
// 调用命名空间 "B" 中的函数 "foo"
\B::foo();
// 调用全局空间中的类 "B" 的 "foo" 方法
// 如果类 "B" 未找到,则尝试自动装载类 "B"
// 当前命名空间中的静态方法或函数
A\B::foo();
// 调用命名空间 "A\A" 中定义的类 "B" 的 "foo" 方法
// 如果类 "A\A\B" 未找到,则尝试自动装载类 "A\A\B"
\A\B::foo();
// 调用命名空间 "A\B" 中定义的类 "B" 的 "foo" 方法
// 如果类 "A\B" 未找到,则尝试自动装载类 "A\B"
?>
|
The namespace of PHP