PHP Command Space usage Daquan

Source: Internet
Author: User
Tags parse error
We know that one of the most explicit purposes in a namespace is to solve the problem of duplicate names, so two functions or classes in PHP are not allowed to have the same name, or there will be a fatal error. In this case, as long as you avoid naming duplicates can be resolved, the most common practice is to contract a prefix.

Purpose of using namespace:

When working on a team project, avoid conflicts with the newly created classes of other members of the team;

As a personal understanding, when using the required classes, you need to require or include, so a class-redefinition error can occur if: two classes of the same named class are introduced. Some PHP frameworks now automatically load (that is, include) all new model classes, so the namespace is used to avoid conflicting duplicate names of the original core classes of your new model class and project framework. (Think about it, conflicts with the newly created class of team members should be avoided by communication, even if the class name should be re-maintained immediately after the incident, to avoid late-generation due to confusion of the understanding of the class resulting in increased maintenance complexity)

Use the method to further understand the purpose of its use.

How to use namespace:

In order to test, I will create 3 files: 1.php and name.php (this file is used to perform the test), will not be described later, please pay attention to the changes in the code.

1.namespace Post-named definitions are case-insensitive

namespace one;
namespace one;
namespace one;

As you can, choose one as your own specifications. (I use the first test in the following code ha)

2. Without defining a namespace, it is understood to use a top-level namespace. New class, you can precede the class with a backslash \ or no addition.

1.phpclass 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 you bring a namespace with the new class, you must use a backslash character instead of a slash.

Memory method: According to the order of the slash in the search is understood as a slash. (sometimes said the backslash, I do not know which direction, previously according to the direction of the left-to-right upward direction of memory, now feel this is too unreliable)

Name.phprequire_once './1.php '; New/person (); Code Error: Parse error:syntax error, unexpected '/'

4. Class when the new class is under the specified namespace, be sure to bring the specified namespace with it.

Without the specified namespace, PHP will look for this class from the top-level namespace in the 2nd place. Remember: This cannot be understood by the top-level namespace that contains all the other namespaces. Instead, the top-level namespace should be completely separate from the other namespace areas.

/1.phpnamespace 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 ' is not found

This is a popular example to understand: Bring the apple (in his hand) with the specified namespace to represent someone, and the top-level namespace represents the apple in the Apple case (in the case). Now, to find someone's apple, take someone's namespace, or you'll find someone's apple from the box, but of course you won't find it.

5. The code after the namespace declaration belongs to this namespace, even if there is an include or require (the emphasis is on the understanding of the latter half of the sentence, see the code specifically).

1.phpnamespace 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's the result here, guess what?

The last line results in an error:

Fatal error:class ' Test\person ' not found

First, here's a comparison with the 2nd:

2nd, I said that when there is no namespace, the new class has no meaning like the backslash.

Here, with namespaces, there is a difference between having and without a backslash.

The last line is replaced

New \person ();

Result Error:

Fatal error:class ' person ' is not found

Then, just talk about the current point.

We can see that the last line of code corresponds to the namespace test and is not affected by the namespace in the Require file.

To further enhance the 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 (); What will be the result here, guess for yourself

Finally, this example refreshes my understanding of require.

According to my previous understanding of require: Before the PHP program executes, it will read into the file specified by require and make it part of the PHP program's Web page. So I often simply understand the substitution, just put the extracted code back in place. Then I tried to put the contents of the 1.php file into the name.php:

Name.phpnamespace test;namespace one;class person{function construct () {  echo ' I am one! ';}} Class person{function construct () {  echo ' I am test! ';}}

Without the new class, the file will be error-free:

Fatal Error:cannot Redeclare class One\person

It seems simple to think of require as a replacement, and it won't work here.

The class name is not included in 6.namespace, even if there is a section with the same name as the class names. New class, you still have to take this part.

Name.phpnamespace test\person;class person{function construct () {  echo ' I am test! ';}

}new \test\person\person (); Person cannot represent class name in namespace

But this is purely superfluous, direct simply point, namespace do not take the class name is good.

7. There can be more than one namespace in a php file and no code in front of the first namespace.

Just say that the first namespace cannot have any code before the namespace can have code before it. This self-test can be done.

Name.phpnamespace Test;echo ' zhai14 '; namespace Zhai;require './1.php ';


Php namespace namespace is over, so let's talk about use.

Purpose of using use:

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

How to use using:

1.new class, you do not need a backslash at the front. In addition, when use does not have as, the shortened namespace defaults to the content after the last backslash.

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 Order of code execution, here the default Animal\cat this namespace new \animal\dog\life (); Ause Animal\dog; Anew Dog\life (); Buse Animal\dog as D; Bnew D\life ();


By comparing A and B lines, you need to be aware of:

When use is used, the new class is preceded by a backslash.

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

By comparing A and B lines, you can understand:

When use does not have as, the shortened namespace defaults to the content after the last backslash. As above:

Use Animal\dog;

Equivalent

Use Animal\dog as dog;

The addition of the class name is not recommended after 2.namespace, but can be used after use.

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, when you add a class name to a use, it is equivalent to changing the class name: Life changed to dog.

The above does not use as dog to error:

Fatal error: Cannot use Animal\dog\life as life because the name was already in use  


Because Cat also has a life class of the same name.

It can be understood that, with use, the class of the nickname can only be owned by the current namespace, and the class is not allowed under 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 above, the use of

Use Animal\dog; Cat

With the above code, I would like to use the purpose effect (shortening the namespace name) of the usage to be obvious.

Briefly summarize:

namespace is the function of dividing the domain, which means that these things belong to a certain namespace.


Believe that you have seen these cases you have mastered the method, more wonderful please pay attention to the PHP Chinese network other related articles!

Related reading:

A method of generating Cartesian product from PHP custom function

PHP custom functions Generate Cartesian product

Multiple arrays to find Cartesian product

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.