Usage of namespace in php

Source: Internet
Author: User

Usage of namespace in php

Now, I feel a little out of date, but I still feel that there are not many people using namespace. It is probably because I am not used to it.

Class organizes functions one by one. namespace can be understood as an orderly organization of classes and functions. I personally think the main advantages of namespace are:

First, better code management

Second, if there are more files, the duplicate names of the class and function can be avoided.

Third, enhanced code readability

1. Define namespace

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

Namespace userCenter;

 

// Php code

 

 

 

Namespace userCenter \ register;

 

// Php code

 

 

 

Namespace userCenter \ login {

 

// Php code

 

}

The namespace cannot be nested or declared multiple times in the same code (only the last one will be recognized ). However, you can define multiple namespace-based codes in the same file. A proper practice is to define a namespace (or the same namespace) for each file ).

2. Call namespace

\ UserCenter \ register; // an absolute call

UserCenter \ login; // relative call

Use userCenter \ register; // Reference Space

Use userCenter \ register as reg; // reference the space and add an alias

3. instance description

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

<? Php

 

 

 

Namespace userCenter;

 

 

 

Function check_username (){

 

Echo "login OK <br> ";

 

}

 

 

 

Class login {

 

Public function save (){

 

Echo "login had saved <br> ";

 

}

 

}

 

?>

 

Regist. class. php

 

 

 

<? Php

 

Namespace userCenter \ regist

 

{

 

Function check_username (){

 

Echo "regist OK <br> ";

 

}

 

 

 

Class regist {

 

Public function save (){

 

Echo "regist had saved <br> ";

 

}

 

}

 

}

 

 

 

?>

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

<? Php

 

 

 

Require "login. class. php ";

 

Require "regist. class. php ";

 

 

 

Use userCenter \ regist; // use to call a space

 

Use userCenter \ regist as reg; // as defines the alias

 

 

 

Echo \ userCenter \ check_username (); // call absolutely

 

 

 

$ Login = new \ userCenter \ login ();

 

Echo $ login-> save ();

 

 

 

Echo regist \ check_username (); // relative call

 

Echo reg \ check_username (); // alias call

 

 

 

$ Regist = new reg \ regist ();

 

Echo $ regist-> save ();

It is better to use it than to call it. It is like adding a prefix to class and function. This makes it clearer.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.