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

PHP namespace namespace usage example analysis

高洛峰
Release: 2023-03-04 15:48:01
Original
1254 people have browsed it

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

common.php:

<?php
class me{
  public function __construct(){
    echo &#39;no namespace&#39;.&#39;<br>&#39;;
  }
}
Copy after login
<?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();
Copy after login

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!

Related labels:
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