Home >Backend Development >PHP Tutorial >PHP namespace namespace usage example analysis

PHP namespace namespace usage example analysis

高洛峰
高洛峰Original
2017-01-06 13:39:061239browse

The examples in this article describe the usage of PHP namespace namespace. Share it with everyone for your reference. The details are as follows:

namespace (namespace) is equivalent to functions and classes. It divides an area. This way, the same class can be required on the same page and the same name can be used. Function
: It is rarely used in projects

name.php:

<?php
//命名要使用复合名称
namespace me\mine;
class me{
  public function __construct(){
    echo &#39;name&#39;.&#39;<br>&#39;;
  }
  public function name(){
    echo &#39;i use space&#39;.&#39;<br>&#39;;
  }
}
//$me = new me();
function me(){
  echo &#39;is me&#39;.&#39;<br>&#39;;
}

common.php:

<?php
class me{
  public function __construct(){
    echo &#39;no namespace&#39;.&#39;<br>&#39;;
  }
}
<?php
//建议使用别名
use me\mine as i;
require &#39;./name.php&#39;;
require &#39;./common.php&#39;;
$me = new i\me();
$me->name();
i\me();
$com = new me();

The running effect is as follows:

PHP namespace namespace usage example analysis

I hope this article will be helpful to everyone in PHP programming.

For more articles related to PHP namespace namespace usage example analysis, please pay attention to the PHP Chinese website!

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