php学习7-- 命名空间

来源:互联网 发布:长江现货铜价 数据接口 编辑:程序博客网 时间:2024/06/05 09:21

命名空间:
1 防止编写的代码于php内部的类/函数/常量或者第三方类/函数/常量之间的名字冲突。
2 为很长的标识符名称创建了一个别名,提高可读性。

定义命名空间:
只有类(包括抽象类和traits),接口,函数和常量,受命名空间的影响。
通过关键字namespace声明,在使用的所有代码前声明(命名空间必须是程序脚本的第一条语句)。declare语句除外。

<?phpnamespace MyProject;const CONNECT_OK = 1;class Connection { /* ... */ }function connect() { /* ... */  }?>当代码含有全局代码时<?phpdeclare(encoding='UTF-8');namespace MyProject {const CONNECT_OK = 1;class Connection { /* ... */ }function connect() { /* ... */  }}namespace { // 全局代码session_start();$a = MyProject\connect();echo MyProject\Connection::start();}?>

PHP 命名空间中的元素使用同样的原理。
类名可以通过三种方式引用:
非限定名称,或不包含前缀的类名称:
例如 $a=new foo();foo::staticmethod();。如果当前命名空间是 currentnamespace,foo 将被解析为 currentnamespace\foo。如果使用 foo 的代码是全局的,不包含在任何命名空间中的代码,则 foo 会被解析为foo。

限定名称,或包含前缀的名称:
例如 $a = new subnamespace\foo(); 或 subnamespace\foo::staticmethod();。如果当前的命名空间是 currentnamespace,则 foo 会被解析为 currentnamespace\subnamespace\foo。如果使用 foo 的代码是全局的,不包含在任何命名空间中的代码,foo 会被解析为subnamespace\foo。

完全限定名称,或包含了全局前缀操作符的名称:
例如, $a = new \currentnamespace\foo(); 或 \currentnamespace\foo::staticmethod();。在这种情况下,foo 总是被解析为代码中的文字名(literal name)currentnamespace\foo。
注意访问任意全局类、函数或常量,都可以使用完全限定名称,例如 \strlen() 或 \Exception 或 \INI_ALL。

<?phpnamespace Foo;function strlen() {}const INI_ALL = 3;class Exception {}//非foo内的方法$a = \strlen('hi'); // 调用全局函数strlen$b = \INI_ALL; // 访问全局常量 INI_ALL$c = new \Exception('error'); // 实例化全局类 Exception?>

PHP支持两种抽象的访问当前命名空间内部元素的方法,NAMESPACE 魔术常量和namespace关键字。

常量NAMESPACE的值是包含当前命名空间名称的字符串。在全局的(也就是没有声明命名空间时),不包括在任何命名空间中的代码,它包含一个空的字符串。
关键字 namespace 可用来显式访问当前命名空间或子命名空间中的元素。它等价于类中的 self 操作符。(也就是可以用来表示当前命名空间的名字)

namespace MyProject;namespace\func();//call function MyProject\func()

使用命名空间:别名/导入

namespace foo;use My\Name as Myname;use My\Age;use Time;use function My\Age\getAge;// 引入函数(可不导入直接别名)use function My\Age\getAge as get;//重新定义函数名use const My\CONSTANT;$a = new namespace\Myname;//foo\Myname;$b = new Myname;//My\Name;Age\func();//My\Age\func();$c = new Time();//Time;$c = new namespace\Time();//foo\Time;get();//My\Age\getAge()echo CONSTANT;//My\CONSTANT一次导入多个use My\Class as Another ,My\Name;$a = new Another();//My\Class;$b = new Name();//My\Name();注意:use必须定义在最外面(global)或者在命名空间声明。importing规则是基于文件的,不会继承于父类的文件。includefile即使用于某个namespace中其内容所对应的命名空间仍然是global。遇到类名的解析时,首先会在命名空间内找,如果找不到就会报错,所以访问系统内部的,或者不在命名空间内的,使用完全限定名称(\)。而对于函数和常量,则会退而使用全局空间中的函数或常量。如果namespace的名字和use 。。as name相同,则解析为use后面的命名空间。一个命名空间中定义另一个命名空间是不允许的。use只能用来引入namespace和类名,函数时可以用use function,常量用use const;不要在命名空间中定义特殊的常量,如:null,ture,false...file1.php<?phpnamespace my\stuff;class MyClass {}?>another.php<?phpnamespace another;class thing {}?>file2.php<?phpnamespace my\stuff;include 'file1.php';include 'another.php';use another\thing as MyClass;$a = new MyClass; // instantiates class "thing" from namespace another,because the MyClass definition in separate file?><?phpnamespace my\stuff;use another\thing as MyClass;class MyClass {} // fatal error: MyClass conflicts with import statement,definition in same file$a = new MyClass;?>
0 0
原创粉丝点击