zoukankan      html  css  js  c++  java
  • php的空间命名

    1、介绍

    当项目越来越大,php的文件越来越多,定义的类型、函数、或者常量就不可避免的出现冲突的情况,特别是在多人协同开发的情况下,这个情况就更不可避免了,这个时候就需要引入php文件的命名空间来解决这个问题

    2、解决方案

    使用namespace关键字来解决,一旦使用了namespace这个关键字声明了,那么之后的所有代码都属于该空间

    3、定义命名空间

     a、子命名空间:

    <?php
    namespace phpphp_check;
    class Check {
        public function __construct() {
            echo 'this is test';
        }
    }
    ?>

     b、全局空间:在namespace之内是局部空间,而namespace之外的是全局空间;

     4、命名空间的使用

    注意:类只在当前空间查找并使用,但是函数,常量先在当前空间找,如果当前空间没有,再去全局空间查找。

    <?php
    namespace phpcheck;
    class Check {
        private $name;
        public function __construct() {
            $this->name = 'this is check';
        }
        public function get() {
            echo $this->name;
        }
    }
    ?>
    <?php
    class Test {
        private $name;
        public function __construct() {
            $this->name = 'this is test';
        }
        public function get() {
            echo $this->name;
        }
    }
    ?>

    调用

    <?php
    namespace phpcall;
    require_once ('./check.php');
    require_once('./test.php');
    class Check {
        private $name;
        public function __construct() {
            $this->name = 'this is call check';
        }
        public function get() {
            echo $this->name;
        }
    }
    
    class Test {
        private $name;
        public function __construct() {
            $this->name = 'this is call test';
        }
        public function get() {
            echo $this->name;
        }
    }
    
    (new phpcheckCheck())->get();
    //输出 this is check
    (new Check())->get();
    //输出 this is call check
    //注意use的使用
    use phpcheck As T;   //通常情况是以空间的最后一个名字为别名
    (new TCheck())->get();
    //输出 this is check
    
    //如果需要访问全局的函数,那么加个即可
    (new Test())->get();
    //输出 this is test
    (new Test())->get();
    //输出 this is call test
    ?>

     注意:1、如果引入全部内容以及里面的类,则使用 use 空间名;2、如果想单纯引入常量:use 空间名常量名; 3、如果想单纯引入函数:use空间名函数名

    <?php
    namespace phpcheck;
    class Check {
        private $name;
        public function __construct() {
            $this->name = 'this is check';
        }
        public function get() {
            echo $this->name;
        }
    }
    
    CONST NAME = 'OK';
    
    function Demo() {
        echo 'this is demo test';
    }
    ?>

    调用

    <?php
    namespace phpcall;
    require_once('./check.php');
    //如果引入全部的类及内容
    use phpcheck As check;
    (new checkCheck())->get();
    checkDemo();
    var_dump(checkNAME);
    
    //单纯引入常量
    use CONST phpcheckNAME;
    var_dump(NAME);
    
    //单纯的引入函数
    use function phpcheckDemo;
    Demo();
    ?>

     5、命名空间的动态语言特性

    介绍:我们可以把命名空间保存到变量中,这就体现了命名空间的动态语言特性

    细节:在保存到变量或常量中,可以把前导的线去除,因为两都在存变量的时候就没有区别了

    check沿用上面的check文件

    <?php
    namespace phpcall;
    require_once('./check.php');
    $check = 'phpcheckCheck';
    (new $check())->get();
    //输出 this is check
    $check1 = 'phpcheckCheck';
    (new $check1())->get();
    //输出 this is check
    
    $demo = 'phpcheckDemo';
    $demo();
    //输出 this is demo test
    
    echo __NAMESPACE__;
    //输出 phpcall
    ?>
  • 相关阅读:
    vi和vim区别
    基本命令
    Ubentu搭建nfs服务器
    Ubuntu + CentOS7 搭建tftp Server
    基于UP-CUP6410点灯实验完成
    搭建服务器(3)
    搭建服务器(2)
    CentOS7.1搭建服务器篇(1)
    关于科台斯k97gprs调试记录(1)
    Arm环境搭建-基于博创科技(CentOS7.0系统安装篇1)
  • 原文地址:https://www.cnblogs.com/rickyctbu/p/11403239.html
Copyright © 2011-2022 走看看