Home  >  Article  >  Backend Development  >  Example analysis of namespace use usage in php, namespace example analysis_PHP tutorial

Example analysis of namespace use usage in php, namespace example analysis_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:00:16891browse

Example analysis of namespace use usage in php, namespace example analysis

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

It feels a bit outdated to say this now, but I feel that not many people use namespaces, probably because they are not used to it.

Class organizes functions one by one, and namespace can be understood as organizing classes, functions, etc. in an orderly manner. Personally, I think the main advantages of namespace are

First, you can better manage the code
Second, with more files, you can avoid duplicate names of classes and functions
Third, code readability is enhanced

1. Define namespace

namespace userCenter;
//php代码
namespace userCenter\register;
//php代码
namespace userCenter\login {
 //php代码
}

Namespaces cannot be nested or declared multiple times in the same code (only the last one will be recognized). However, you can define multiple namespaced codes in the same file. It is more appropriate to define a namespace for each file (it can be the same namespace).

2. Call namespace

\userCenter\register; //绝对调用
userCenter\login; //相对调用
use userCenter\register; //引用空间
use userCenter\register as reg; //引用空间并加别名

3. Example

login.class.php

<&#63;php
namespace userCenter;
function check_username(){
 echo "login OK<br>";
}
class login{
 public function save(){
 echo "login had saved<br>";
 }
}
&#63;>

regist.class.php

<&#63;php
namespace userCenter\regist
{
 function check_username() {
 echo "regist OK<br>";
 }
 class regist{
 public function save(){
 echo "regist had saved<br>";
 }
 }
}
&#63;>

test.php

<&#63;php
require "login.class.php";
require "regist.class.php";
use userCenter\regist; //使用use调用空间
use userCenter\regist as reg; //as定义别名
echo \userCenter\check_username(); //绝对调用
$login = new \userCenter\login();
echo $login->save();
echo regist\check_username(); //相对调用
echo reg\check_username(); //别名调用
$regist = new reg\regist();
echo $regist->save();

Using use is better than absolute calling. It is like adding a prefix to class, function, etc., so that it looks clearer.

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

Articles you may be interested in:

  • thinkphp autoload namespace custom namespace
  • Dynamic access and usage skills of PHP namespace (namespace)
  • Basics and examples of using PHP namespace (namespace)
  • A concise tutorial on PHP namespace (Namespace)
  • Detailed explanation of the use of PHP namespace (Namespace)
  • thinkphp namespace usage Detailed explanation of examples
  • Detailed introduction to namespaces in PHP
  • A brief analysis of related concepts of namespaces in PHP
  • Detailed explanation of PHP namespace learning
  • PHP namespace Example description
  • PHP 5.3 new feature namespace rule analysis and advanced functions

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1094750.htmlTechArticleExample analysis of namespace use usage in php, namespace example analysis This article describes the usage of namespace use in php. I share it with you for your reference, the details are as follows: Now I feel like this...
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