In this blog post, let’s take a look at the relationship between namespaces and public spaces, and the issues that need to be paid attention to when using them. We will demonstrate them through two PHP files.
Note that 1.php cannot include 2.php, and at the same time, 2.php include 1.php, an infinite loop will report an error.
1.php:
<?php /** * 命名空间 公共空间 访问方式 * 命名空间 优先级大于 公共空间 */ namespace shenyang; class Animal{ public $obj = 'dog<br>'; static $name = '大黄<br>'; } function getmsg(){ echo "辽沈<br>"; } const NM = "iwanghang1<br>"; include("./2.php"); getmsg(); // 打印结果:辽沈 (由此可见,引入的空间,对当前空间没有影响) echo \NM; // 打印结果:iwanghang2 (\ 公共空间 访问方式) echo NM; // 打印结果:iwanghang1 (假如,当前空间没有NM,就会去公共空间查找NM,)
2.php:
<?php function getmsg(){ echo "北京"; } const NM="iwanghang2<br>"; /** * 注意,不能1.php include 2.php,同时,2.php include 1.php,死循环会报错 * 当我们测试下面的代码的时候,要注释掉1.php中的“include("./2.php");” */ include("./1.php"); echo NM; // 不推荐的写法 echo \NM; // 打印结果:iwanghang2 echo \shenyang\NM; // 打印结果:iwanghang1
The above is the PHP development (32)-ThinkPHP5.0 (4) namespace With the content of public space-PhpStorm, please pay attention to the PHP Chinese website (m.sbmmt.com) for more related content!