搜索
首页php教程php手册php中命名空间namespace用法介绍

php中命名空间namespace用法介绍

Jun 13, 2016 am 09:03 AM
namespacephp介绍命名用法空间

php中命名空间namespace用法介绍

   现在说这个,感觉有点过时了,但是感觉用namespace的人还是不多,估计还是因为不习惯吧。

  class把一个一个function组织起来,namespace可以理解成把一个一个class,function等有序的组织起来。个人觉得,namespace的主要优势有

  第一,可以更好的管理代码

  第二,文件一多,可以避免class,function的重名

  第三,代码可读性增强了

  1,定义namespace

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

namespace userCenter;

 

//php代码

 

 

 

namespace userCenterregister;

 

//php代码

 

 

 

namespace userCenterlogin {

 

//php代码

 

}

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
namespace userCenter;   //php代码       namespace userCenterregister;   //php代码       namespace userCenterlogin {   //php代码   }

  命名空间不能嵌套或在同一代码处声明多次(只有最后一次会被识别)。但是,你能在同一个文件中定义多个命名空间化的代码,比较合适的做法是每个文件定义一个命名空间(可以是相同命名空间)。

  2,调用namespace

  userCenterregister; //绝对调用

  userCenterlogin; //相对调用

  use userCenterregister; //引用空间

  use userCenterregister as reg; //引用空间并加别名

  3,实例说明

  login.class.php

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

 

 

 

namespace userCenter;

 

 

 

function check_username(){

 

echo "login OK
";

 

}

 

 

 

class login{

 

public function save(){

 

echo "login had saved
";

 

}

 

}

 

?>

 

regist.class.php

 

 

 

 

namespace userCenterregist

 

{

 

function check_username() {

 

echo "regist OK
";

 

}

 

 

 

class regist{

 

public function save(){

 

echo "regist had saved
";

 

}

 

}

 

}

 

 

 

?>

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
      namespace userCenter;       function check_username(){   echo "login OK
";   }       class login{   public function save(){   echo "login had saved
";   }   }   ?>   regist.class.php         namespace userCenterregist   {   function check_username() {   echo "regist OK
";   }       class regist{   public function save(){   echo "regist had saved
";   }   }   }       ?>

测试.php

表> 使用use,比绝对调用要好一点,好比给class,function等加了一个出口,这样看起来就比较清楚了

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

 

 

 

require "login.class.php";

 

require "regist.class.php";

 

 

 

use userCenterregist; //使用use调用空间

 

use userCenterregist as reg; //as定义别名

 

 

 

echo userCentercheck_username(); //绝对调用

 

 

 

$login = new userCenterlogin();

 

echo $login->save();

 

 

 

echo registcheck_username(); //相对调用

 

echo regcheck_username(); //别名调用

 

 

 

$regist = new regregist();

 

echo $regist->save();

1

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
      需要“login.class.php”;   需要“regist.class.php”;       使用userCenter注册; //使用use调用空间   使用 userCenterregist 作为 reg; //as定义别名       echo userCentercheck_username(); //绝对调用       $login = new userCenterlogin();   echo $login->save();       echo registcheck_username(); //相对调用   echo regcheck_username(); //别名调用       $regist = new regregist();   echo $regist->save();
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),