Explanation of the php namespace (1) and the php namespace
The namespace function of php has been available for a long time, but it has never been used for further research. This time, I rushed to the Manual to make a summary and help myself improve and improve, I originally prepared an article, but I found that there were quite a lot of content. It was too long to put them together, so it would be better to divide them into two blogs.
I. namespace concept: a namespace is a way to encapsulate things, similar to directories and files.
The namespace solves the problem (the manual is also clearly written, which is simplified according to your own understanding ):
1: resolve conflicts between classes, constants, and functions written by programmers and internal or third parties in php.
2: Create an alias to help solve the problem of too long class, constant, and function names, and improve code readability. In addition, the name is too long because it is usually caused by first-class problems.
Ii. How to define a namespace
1: The namespace is declared with the keyword namespace. The namespace must be located before other code, including any non-php code and blank characters (except for the declare keyword of php). Otherwise, a fatal error is thrown.
For example:
<?php namespace Index; ?>
Note 1: If no code or blank characters exist before the namespace, but fatal error still occurs, this should be caused by the bom header. You can remove the bom header.
NOTE 2: although all valid php code can be placed in a namespace, only classes (abstract classes and traits) and interfaces, constants, and functions are affected by the namespace.
2: The relationship with directories and files is similar. The PHP namespace allows you to specify a hierarchical namespace name. Therefore, the namespace name can be defined in a hierarchical manner. The separator is \.
For example:
<?php namespace Index\Col\File; define('MESSAGE','hello world'); ?>
3: A file can define multiple namespaces. There are two types of syntax defined: simple combination syntax and braces syntax, another file defines the use of multiple namespaces. It is generally a scenario where multiple files are merged into one file, but it is better not to do this because it increases the complexity of the Code, the readability is reduced. Generally, this is not necessary.
Simple combination Syntax:
<?php namespace Index; const INSTANCE=1; namespace Col; const INSTANCE=2; ?>
Braces Syntax: multiple namespaces in a file. If you need to write code that is not a namespace, you can only use the braces syntax, in addition, the non-namespace Code uses namespace to declare a namespace without a name, and then uses braces:
<? Php/* namespace Index */namespace Index {const INSTANCE = 1;}/* namespace Col */namespace Col {const INSTANCE = 2 ;} /* globally non-namespace Code */namespace {const INSTANCE = 3 ;}?>
4: multiple different files can define the same namespace. That is to say, the content of the same namespace can be stored in multiple different files separately. This is not an example here.
Iii. namespace recognition principles
There are three scenarios for namespace usage. The instructions in the manual are detailed but may be messy due to translation problems. Here I will simplify it and use my own examples to sort it out:
1: There is no qualified name, that is, the name of the class, constant, function, and interface to be read is directly used, in this case, the class, constant, function, and interface Name Of The namespace to which the content belongs will be read. However, if there is no relevant data in the namespace, if it is a class or interface name, fatal error will be returned. If it is a function or constant, the global function and constant will be automatically read. If there is no global function, the fatal error will be reported.
The following is an example:
<? Php/* code for a Global Non-namespace */namespace {const INSTANCE = 1; function test () {echo 1;} class foo {static function fool () {echo 1 ;}} var_dump (INSTANCE); // The output is 1 test (); // The output is 1 foo: fool (); // output 1}/* namespace Index */namespace Index {const INSTANCE = 2; function test () {echo 2;} class foo {static function fool () {echo 2 ;}} var_dump (INSTANCE); // The output is 2 test (); // The output is 2 foo: fool (); // output 2}/* namespace Col */namespace Col { Const INSTANCE = 3; function test () {echo 3;} class foo {static function fool () {echo 3 ;}} var_dump (INSTANCE ); // The output is 3 test (); // output 2 foo: fool (); // output 2}?>
In the preceding example, no name is specified for the output in each namespace. Therefore, the corresponding data value set in the current namespace is obtained.
If the current namespace is not set, the function and constant will read the corresponding data value of the global setting. If there is no corresponding global value, a fatal error will be reported, and a fatal error will be reported directly for the class and interface, as shown in the following code.
<? Php/* code for a Global Non-namespace */namespace {const INSTANCE = 1; function test () {echo 1;} class foo {static function fool () {echo 1 ;}} var_dump (INSTANCE); // The output is 1 test (); // The output is 1 foo: fool (); // output 1}/* namespace Index */namespace Index {var_dump (INSTANCE); // The output is 1 test (); // output 1 foo :: fool (); // fatal error}?>
2: The name can be either a qualified name containing a prefix or a globally qualified name. The two types are separated separately in the manual, but I think they can be combined into a qualified name, but the former has no global limitation, and the latter has a global limitation.
① A qualified name that contains a prefix. This prefix can have multiple or one level, but the leftmost cannot be a globally qualified word, in this case, the data corresponding to the qualified name is read in the namespace where the code is located, that is:
The namespace \ prefix limitation \ name to read. If the Code does not have a namespace globally, you can directly read the code by prefix limitation name, that is, prefix limitation \ name.
Instance code:
<? Php/* namespace Col \ Index */namespace Col \ Index {const INSTANCE = 1;}/* namespace Index */namespace Index {const INSTANCE = 2 ;} /* namespace Col */namespace Col {const INSTANCE = 3; var_dump (Index \ INSTANCE ); // print the result that 1 reads Col \ Index \ INSTANCE}/* globally non-namespace Code */namespace {const INSTANCE = 4; var_dump (Index \ INSTANCE ); // The printed result is that 2 reads Index \ INSTANCE}?>
② Globally qualified prefix name: that is, a globally qualified prefix name with the global operator \ On the leftmost side. Of course, you can also directly add the global operator \ with the name without the prefix limitation. However, after the global operator is added, it is the same as the absolute path in the directory and will only be read according to the settings after the global limitation.
The specific example is as follows:
<? Php/* namespace Col \ Index */namespace Col \ Index {const INSTANCE = 1;}/* namespace Index */namespace Index {const INSTANCE = 2 ;} /* namespace Col */namespace Col {const INSTANCE = 3; var_dump (\ Index \ INSTANCE ); // The printed result is that 2 reads the Index \ INSTANCE}/* Global Non-namespace Code */namespace {const INSTANCE = 4; var_dump (\ Index \ INSTANCE ); // print the result that 2 reads Index \ INSTANCE} namespace Lin {const INSTANCE = 5; var_dump (\ INSTANCE); // print the result that 4 reads INSTANCE, is a global non-life If the INSTANCE in the namespace does not have the global operator \, the read will be the Lin \ INSTANCE = 5} of the current namespace?>
4. Escape a namespace in a string
Sometimes namespaces are used in strings. If single quotes are not interpreted by the compiler, there is no problem, but if they are double quotes, there will be some unexpected situations, you need to know that the content in double quotation marks must be interpreted by the compiler and then output, while the interpretation in the compiler may cause ambiguity.
For example, "index \ name" indicates that \ n is interpreted as a line break, which may cause many exceptions.
Therefore, we recommend that you use single quotation marks (single quotation marks) if the namespace is to be placed in strings. One is efficiency and the other is security. If double quotation marks are used, you must add an escape character to avoid ambiguity, for example, "index \ name.
Double quotation marks:
<? Php/* code for a Global Non-namespace */namespace Index \ Name {class foo {function _ construct () {echo 2 ;}}} namespace {$ a = "Index \ Name \ foo"; // escape \ with \, so it can run normally, however, if the escape is removed, the Class 'index \ Nameoo 'will be reported, because/f is interpreted as a break of $ obj = new $ ;}
This part is temporarily here because of the length. The next article mainly summarizes the use of namespace and _ NAMESPACE _ In The namespace, as well as the use of aliases.