Home >Backend Development >PHP Tutorial >Introduction to the usage of namespace in php_PHP tutorial

Introduction to the usage of namespace in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:53:06942browse

Introduction to the usage of namespaces in PHP

Speaking of this now, it feels a bit outdated, 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

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 code namespace userCenterregister; //php code namespace userCenterlogin { //php code }

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

userCenterregister; //Absolute call

userCenterlogin; //Relative call

Use userCenterregister; //Reference space

Use userCenterregister as reg; //Reference space and add alias

 3, Example

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
"; } class login{ public function save(){ echo "login had saved
"; } } ?> regist.class.php "; } class regist{ public function save(){ echo "regist had saved
"; } } } ?>

  test.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

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

45 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();
  使用use,比绝对调用要好一点,好比给class,function等加了一个前缀,这样看起来就比较清楚了 http://www.bkjia.com/PHPjc/1004541.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1004541.htmlTechArticlephp中命名空间namespace用法介绍 现在说这个,感觉有点过时了,但是感觉用namespace的人还是不多,估计还是因为不习惯吧。 class把一个一个...
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