Summary of the usage of namespace and use in the PHP Command Space, namespaceuse

Source: Internet
Author: User
Tags parse error

Summary of the usage of namespace and use in the PHP Command Space, namespaceuse

The most explicit purpose of a namespace is to solve the problem of duplicate names. In PHP, two functions or classes cannot have the same name. Otherwise, a fatal error occurs. In this case, you only need to avoid repeated names. The most common practice is to define a prefix.

Purpose of using namespace:

Avoid conflicts with classes created by other team members during team cooperation projects; avoid conflicts between classes created before and after when individuals are in charge of projects;

According to my personal understanding, when using the required classes, you need to first introduce require or include. Therefore, the premise of a class redefinition error is that two classes with the same name are introduced. Currently, some php frameworks automatically load (I .e. include) all new model classes. To avoid duplicate name conflicts between the new model class and the native core class of the project framework, namespace is used. (If you think about it, you should avoid conflicts with the classes created by the team members through communication. Even after the incident, you should re-adjust the class name for immediate maintenance, avoid increasing maintenance complexity due to obfuscation of classes in the future)

Use the methods to further understand its purpose.

How to Use namespace:

For testing, I will create three files: 1. php and name. php (this file is used for testing), which will not be described later. Please pay attention to the code changes.

1. The namespace naming definitions are case-insensitive.

Namespace one;
Namespace One;
Namespace ONE;

You can select either of the above writing methods as your own specification. (I will use the first method to test the code later)

2. If no namespace is defined, it is understood as using a top-level namespace. When you create a new class, you can add the backslash \ in front of the class, or do not add.

// 1.php class Person {function _ construct () {echo 'I am one! ';}} // Name. phprequire_once'./1. php'; new Person (); // output I am one !; New \ Person (); // output I am one !;

3. When a new class is added with a namespace, a backslash must be used between them, rather than a backslash.

Memory method: It is understood as a forward slash in the order of %. (Sometimes I don't know the direction of the backslash. I used to remember the direction from left to right. Now I feel this is too unreliable)

// Name. phprequire_once './1. php'; new/Person (); // code error: Parse error: syntax error, unexpected '/'

4. When a class is in a specified namespace and a new class, the specified namespace must be included.

The specified namespace is not included. php will find this class from the top-level namespace according to the 2nd point. Remember: it cannot be understood that the top-level namespace contains all other namespaces. The top-level namespaces should be completely separated from other namespaces.

// 1.php namespace one; class Person {function _ construct () {echo 'I am one! ';}} // Name. phprequire_once'./1. php'; new \ one \ Person (); // output I am one !; New \ Person (); // code error: Fatal error: Class 'person 'not found

You can take this example to understand: a namespace with a specified namespace represents someone's apple (in his hand), and a top namespace represents the apple (in the box) in the apple box ). Now, if you want to find someone's apple, you can take someone's namespace with it. Otherwise, you will find someone's apple from the box. Of course, the result will not be found.

5. The code after the namespace declaration belongs to this namespace, even if there is include or require, it does not affect (the focus is on understanding the last half sentence, depending on the Code ).

// 1.php namespace one; class Person {function _ construct () {echo 'I am one! ';}} // Name. phpnamespace test; require'./1. php'; new \ one \ Person (); // output I am one !; New Person (); // What is the result here? Guess it.

An error is returned for the last row:

Fatal error: Class 'test\Person' not found

First, compare it:

, I said, when there is no namespace, when there is a new class, there is no backslash meaning the same.

Here, with the namespace, there is a different meaning with and without a backslash.

Replace the last line

new \Person(); 

Error:

Fatal error: Class 'Person' not found

Next, let's talk about the current point.

We can find that the namespace corresponding to the last line of code is test, and it is not affected by the namespace in the require file.

For further verification, I modified the name. php file as follows:

// Name. phpnamespace test; require './1. php'; class Person {function _ construct () {echo' I am test! ';}} New \ one \ Person (); // output I am one !; New Person (); // you can guess what the result is.

Finally, this example refreshes my understanding of require.

According to my previous understanding of require: Before executing the PHP program, it will first read the file specified by require to make it part of the PHP program webpage. So I often simply think of replacement, but I just need to put the code that is extracted back to the original place. Then I tried to put the content of the 1. php file in name. php:

//name.phpnamespace test;namespace one;class Person{ function __construct(){  echo 'I am one!'; }}class Person{ function __construct(){  echo 'I am test!'; }}

No new class is required, and an error is reported for this file:

Fatal error: Cannot redeclare class one \ Person

It seems that simply interpreting require as a replacement will not work here.

6. The namespace does not contain the class name. Even if there is a part with the same name as the class name, it does not represent the class. When you create a new class, you must attach this part.

// Name. phpnamespace test \ person; class Person {function _ construct () {echo 'I am test! ';}} New \ test \ person \ Person (); // The person in the namespace cannot represent the class name.

However, this is purely superfluous, just click it. do not include a class name in the namespace.

7. multiple namespaces can exist in a PHP file. No code exists before the first namespace.

Only that there is no code before the first namespace, and there can be code before the namespace. You can test it by yourself.

//name.phpnamespace test;echo 'zhai14';namespace zhai;require './1.php'; 

The namespace of the php namespace has come to an end. Next, let's talk about the purpose of use.

Purpose:

When the namespace string is too long, use can shorten the namespace accordingly.

Usage:

1. When a new class is created, a backslash is not required at the beginning. In addition, when there is no as after use, the shortened namespace is the content after the last backslash by default.

// Name. phpnamespace animal \ dog; class Life {function _ construct () {echo 'dog life! ';}} Namespace animal \ cat; class Life {function _ construct () {echo 'cat life! ';}} New Life (); // according to the code execution order, here the default namespace animal \ cat is new \ animal \ dog \ Life (); // Ause animal \ dog; // anew dog \ Life (); // Buse animal \ dog as d; // bnew d \ Life ();

For comparison of lines A and B, note the following:

After use, when the new class is used, there is no backslash at the beginning.

When use is not used, the front of the namespace is a backslash.

Through the comparison of lines a and B, you can understand:

When there is no as after use, the shortened namespace is the content after the last backslash by default. For example:

Use animal \ dog;

Equivalent

Use animal \ dog as dog;

2. You are not recommended to add a class name after namespace, but you can use it later.

//name.phpnamespace animal\dog;class Life{ function __construct(){  echo 'dog life!'; }}namespace animal\cat;class Life{ function __construct(){  echo 'cat life!'; }}use animal\dog\Life as dog; new dog(); 

As shown above, adding the class name after use is equivalent to changing the class name from "Life" to "dog.

The above error will be reported without the use of as dog:

Fatal error: Cannot use animal \ dog \ Life as Life because the name is already in use
Because cat also has a Life class with the same name.

It can be understood that after use, the class corresponding to this nickname can only be occupied by the current namespace, and the class cannot exist in other namespaces.

//name.phpnamespace animal\dog;class Life{ function __construct(){  echo 'dog life!'; }}class Dog{ function __construct(){  echo 'dog in dog!'; }}namespace animal\cat;// class Dog{// function __construct(){//  echo 'dog in cat!';//  }// }class Life{ function __construct(){  echo 'cat life!'; }}use animal\dog; new dog\Dog(); 

As shown above

Use animal \ dog;
Cat

Through the above code, I want to use the purpose of use (shorten the namespace name) is very obvious.

Summary:

Namespace is the role of Domain Division, which indicates that these things belong to a namespace.

Use is the role of a nickname. It can save a lot of effort for both writing and speaking.

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.