PHP Development(32)-ThinkPHP5.0(4) Namespace and Public Space-PhpStorm

黄舟
Release: 2023-03-06 10:40:02
Original
1722 people have browsed it

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 = &#39;dog<br>&#39;;
        static $name = &#39;大黄<br>&#39;;
    }

    function getmsg(){
        echo "辽沈<br>";
    }

    const NM = "iwanghang1<br>";

    include("./2.php");

    getmsg(); // 打印结果:辽沈 (由此可见,引入的空间,对当前空间没有影响)

    echo \NM; // 打印结果:iwanghang2 (\ 公共空间 访问方式)
    echo NM; // 打印结果:iwanghang1 (假如,当前空间没有NM,就会去公共空间查找NM,)
Copy after login

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
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template