php命名空间namespace应用

命名空间的实现跟我们系统目录文件的组织结构形式类似 , 一个目录下只能存在一个db.php文件。namespace关键字就是来定义类的空间区分。
受命名空间影响的类型:类、接口、函数、常量 , 当然define()定义的常量是全局的,不受空间影响。如果使用命名空间定义常量要用 const

  • 注:
    \ 一个反斜杠是指全局命名空间。\venter 这个是指全局命名空间下的其他子命名空间。但是这种情况只是类存在, 因为全局空间函数和常量在子空间调用的时候,如果空间内找不到会继续到全局空间寻找所以子空间内使用全局空间的函数变量, \ 加或不加功能一样。 如果优化的话应该加上 \ 提高效率。
    例如:
  • venter->session->db.php
  • venter->db.php
1.namespace的常规使用

以下为DEMO示例目录结构

-namespace
	-App
		-AppIndex.php
	-lib
		-LibIndex.php
	-main.php
	/**
	 * main.php
	 * 通过测试如果不定义命名空间两个Index类包下面这个错误,无法重新声明Index类
	 * Fatal error: Cannot redeclare class Index in D:\phpStudy\WWW\namespace\lib\LibIndex.php
	 */
	include_once ('App/AppIndex.php');
	include_once ('lib/LibIndex.php');
	/**
	 * AppIndex.php 
	 */
	namespace UserApp;
	class Index
	{
	}
	/**
	 * LibIndex
	 */
	namespace UserLib;
	class Index
	{
	}
没有载入的命名空间不能调用,命名空间是全局的属性

通过加载入口文件main.php载入空间,才能在UserApp\Index里面调用另一个空间的类

输出结果为:这是 UserApp\Index这是 UserLib\Index

	/**
	 * main.php
	 */
	include_once ('App/AppIndex.php');
	include_once ('lib/LibIndex.php');
	new \UserApp\Index();
	/**
	 * AppIndex.php 
	 */
	namespace UserApp;
	class Index
	{
	    public function __construct()
	    {
	        echo '这是 UserApp\Index';
	        new \UserLib\Index();
	    }
	}
	/**
	 * LibIndex
	 */
	namespace UserLib;
	class Index
	{
	    public function __construct()
	    {
	        echo '这是 UserLib\Index';
	    }
	}
2. use的使用

常规导入类的名称过于繁琐 , 所以php中提供了一个关键字 use 来导入相应命名空间的类,尤其是导入函数和常量,普通方式不能导入函数常量

	/**
	 * main.php
	 */
	include_once ('App/AppIndex.php');
	include_once ('lib/LibIndex.php');
	//use 导入空间
	use  UserApp\Index;
	//这里可以去掉路径前缀直接使用Index()
	new Index();
	/**
	 * AppIndex.php 
	 */
	namespace UserApp;
	class Index
	{
	    public function __construct()
	    {
	        echo '这是 UserApp\Index';
	        new \UserLib\Index();
	    }
	}
	/**
	 * LibIndex
	 */
	namespace UserLib;
	class Index
	{
	    public function __construct()
	    {
	        echo '这是 UserLib\Index';
	    }
	}

注意:PHP 7 之前版本需要使用多次 use

use some\namespace\ClassA; 
use some\namespace\ClassB; 
use some\namespace\ClassC as C; 

use function some\namespace\fn_a; 
use function some\namespace\fn_b; 
use function some\namespace\fn_c; 

use const some\namespace\ConstA; 
use const some\namespace\ConstB; 
use const some\namespace\ConstC; 

PHP 7+ 之后版本可以使用一个 use 导入同一个 namespace 的类

use some\namespace\{ClassA, ClassB, ClassC as C}; 
use function some\namespace\{fn_a, fn_b, fn_c}; 
use const some\namespace\{ConstA, ConstB, ConstC}; 
3. 别名as 的使用

如果在空间内use 导入另一个空间,并且两个空间类名相同, 那么为了避免这种类名冲突的问题出现php 里面设置一个as 别名的功能

	/**
	 * main.php
	 */
	include_once ('App/AppIndex.php');
	include_once ('lib/LibIndex.php');
	use  UserApp\Index;
	new Index();
	/**
	 * AppIndex.php 
	 */
	namespace UserApp;
	//这里直接将类设置别名为LibIndex
	use UserLib\Index as LibIndex;
	class Index
	{
	    public function __construct()
	    {
	        echo '这是 UserApp\Index';
	        //此处直接调用别名就ok
	        new LibIndex();
	    }
	}
	/**
	 * LibIndex
	 */
	namespace UserLib;
	class Index
	{
	    public function __construct()
	    {
	        echo '这是 UserLib\Index';
	    }
	}
4.use导入函数、常量
	/**
	 * main.php
	 */
	include_once ('App/AppIndex.php');
	include_once ('lib/LibIndex.php');
	//use 导入空间
	use  UserApp\Index;
	new Index();
	/**
	 * AppIndex.php 
	 */
	namespace UserApp;
	use UserLib\Index as LibIndex;
	//此处调用UserLib空间的函数
	use function UserLib\userLib;//function关键字要在php5.6以上才能用
	use  UserLib\userLib;//普通导入
	//常量的方式类似也可以不用关键词
	use const UserLib\JXLV;
	class Index
	{
	    public function __construct()
	    {
	        echo '这是 UserApp\Index';
	        new LibIndex();
	        
	        userLib();//function关键字导入的函数调用方式
	        \UserLib\userLib();//普通导入函数调用方式
	        echo JXLV;
	    }
	}
	/**
	 * LibIndex
	 */
	namespace UserLib;
	class Index
	{
	    public function __construct()
	    {
	        echo '这是 UserLib\Index';
	    }
	}
	//此处是空间的函数
	function userLib()
	{
	    echo '我是userlib的函数';
	}

猜你喜欢

转载自blog.csdn.net/qq_39043923/article/details/88791538