PHP development (31)-ThinkPHP5.0 (3) multi-level namespace-PhpStorm

黄舟
Release: 2023-03-06 10:38:01
Original
1145 people have browsed it

Compared with the namespace in the previous blog post, it is just one more layer of relative paths. The only thing to explain is the use of use to import a namespace.

<?php
    /**
     * 多级命名空间
     */
   namespace beijing\haidian;

   class Animal{
       public $obj=&#39;dog<br>&#39;;
       static $name=&#39;大黄<br>&#39;;
   }

   function getmsg(){
       echo &#39;北京海淀<br>&#39;;
   }

   namespace shanghai\putuo;

   class Animal{
       public $obj=&#39;pig<br>&#39;;
       static $name=&#39;哼哼<br>&#39;;
   }

   function getmsg(){
       echo &#39;上海普陀<br>&#39;;
   }


   /**
    * 访问方式
    */
   $animal = new Animal();
   $animal2 = new \beijing\haidian\Animal();
   echo $animal->obj; // 打印结果:pig
   echo $animal2->obj; // 打印结果:dog

   echo Animal::$name; // 打印结果:哼哼
   echo \beijing\haidian\Animal::$name; // 打印结果:大黄

    /**
     * 访问方式:3、限定名称访问方式(相对路径)
     * use 导入一个命名空间
     */
    use beijing\haidian;
    haidian\getmsg(); // 打印结果:北京海淀
    $animal3 = new haidian\Animal();
    echo $animal3->obj; // 打印结果:dog
    echo haidian\Animal::$name; // 打印结果:大黄
Copy after login

The above is the content of PHP Development (31)-ThinkPHP5.0 (3) Multi-level Namespace-PhpStorm. For more related content, please pay attention to the PHP Chinese website (www.php. cn)!


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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!