Home  >  Article  >  Backend Development  >  PHP development (31)-ThinkPHP5.0 (3) multi-level namespace-PhpStorm

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

黄舟
黄舟Original
2017-03-03 10:05:071133browse

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; // 打印结果:大黄

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)!


Statement:
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