One: namespace concept: Namespaces are a way of encapsulating things, similar to directories and files.
namespace to solve the problem (also written in the manual is very clear, the following in accordance with their own understanding Simplified):
1: Resolves a case where the program writer writes its own classes, constants, functions, and internal or third-party name collisions within PHP.
2: Create aliases, help solve the case of class, constant, function name too long, help improve the readability of the code, in addition to the long name is usually because to alleviate the first kind of problems caused.
Two: How to define namespaces
1: The namespace is declared with the keyword namespace, and the namespace must precede other code, including any non-PHP code and whitespace characters (except for PHP's declare keyword), or a fatal error will be thrown.
For example:
<?php namespace Index;?>
Note 1: If the namespace namespace before any code and whitespace characters, but still appear fatal error, this should be due to the BOM header, remove the BOM header can be.
NOTE 2: Under namespaces, although all valid PHP code can be placed, only classes (abstract classes and traits) and interfaces, constants, and functions that are affected by the namespace.
2: As with directories and files, the PHP namespace allows you to specify the name of a hierarchical namespace. Therefore, the name of the namespace can be defined in a hierarchical way, with a delimiter of \.
For example:
<?php namespace Index\col\file; Define (' MESSAGE ', ' Hello World ');?>
3: A file can be defined more than one namespace, the definition of two types of syntax, one is a simple combination of syntax, the other is the form of curly braces syntax, and another file definition of multiple namespaces is generally used to combine multiple files into a single file scene, but it is best not to be the last resort, Because this increases the complexity of the code, readability is reduced, and the general situation is not necessary for this use.
Simple combination Syntax:
<?php namespace Index; Const instance=1; namespace Col; Const instance=2;?>
Curly brace syntax, a file multiple namespaces, if you also need to write the code of the non-namespace, you can only use the curly brace syntax, and non-namespace code with namespace declare a namespace without a name, and then use curly braces:
<?php/* Namespace index*/namespace index{ const instance=1;} /* Namespace col*/namespace col{ const instance=2;} /* Global Non-namespace code */namespace { const instance=3;}?>
4: Many different files can define the same namespace, that is, the contents of the same namespace can be stored in a number of different files, here is not an example.
Three: The recognition principle of namespaces
The use of namespaces in three cases, the manual is actually said in detail but probably because the translation problem leads to some messy, here I simplify the example of their own to comb:
1: There is no qualified name, that is, directly using the class, constant, function, interface name to read, this will read the contents of the namespace class, constant, function, interface name, but if there is no relevant data within the namespace, if the class and interface names will return fatal error, If the functions and constants are automatically read global functions and constants, fatal error will be reported if they are not in the global.
Here are some examples:
<?php/* Global Non-namespace code */namespace { const instance=1; function test () { echo 1; } Class foo{ static function Fool () { echo 1; } } Var_dump (INSTANCE); The print is 1 test (); Output 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 print is 2 test (); Output 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 print is 3 test (); Output 2 foo::fool (); Output 2}?>
The above example does not have a qualified name in each namespace, so the corresponding data values set in the current namespace are obtained.
If the current namespace is not set, the functions and constants will read the corresponding data values of the global settings, the global does not correspond to the fatal error, the class and interface will directly report fatal error, as shown in the following code.
<?php/* Global Non-namespace code */namespace { const instance=1; function test () { echo 1; } Class foo{ static function Fool () { echo 1; } } Var_dump (INSTANCE); The print is 1 test (); Output 1 foo::fool (); Output 1 } /* namespace index*/namespace index{ var_dump (INSTANCE); The print is 1 test (); Output 1 foo::fool (); Fatal error }?>
2: Qualified name, divided into two cases, one is a qualified name containing the prefix case, one is the case of containing the globally qualified name. The manuals separate the two separately, but I think the two can be combined together to say that they all have a qualified name, except that the former has no global qualification and the latter has global qualification.
① contains the qualified name of the prefix, which can have multiple or one level, but the leftmost cannot be a \ Global qualifier, which reads the namespace of the code and the corresponding data for the prefix qualified name, which is:
The namespace \ prefix qualified \ Name to read, if the code is not global namespace, then directly with the prefix qualified name to read, that is: prefix qualification \ name to read.
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); The print is 1 read col\index\instance}/ * Global Non-namespace code */namespace { const instance=4; Var_dump (index\instance); The print is 2 read index\instance} ?>
② Global qualified prefix name: that is, there is a global operator \ to decorate the prefix qualified name, of course, there can be no prefix to qualify the direct global operator \ Plus the name is also possible. However, the global operator, like the absolute path in the directory, is read only after the global qualification is set.
The concrete examples are 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 print is 2 read index\instance}/ * Global Non-namespace code */namespace { const instance=4; Var_dump (\index\instance); The print out is 2 read index\instance} namespace lin{ const instance=5; Var_dump (\instance); The print is 4 read INSTANCE, is the global non-namespace INSTANCE, if there is no global operator \, read will be the current namespace lin\instance=5} ?>
IV: Escape of namespaces in strings
Sometimes the namespace will be used in the string, if the single quotation marks will not be interpreted by the compiler, so there is no problem, but if it is double quotation marks, then there will be some unexpected situation, you should know that the contents of the double quotation marks need to be interpreted by the compiler and then output, and \ In the compiler interpretation is prone to ambiguity.
For example, "Index\name" here will be interpreted as a newline, in addition to a lot of this caused the unexpected situation.
So generally we recommend namespaces if you want to put in a string, it is best to use single quotes, one is efficiency, the other is security, if you use double quotes, you must add a \ To escape the ambiguity, such as "index\\name" so there is no problem.
Here are some examples of double quotes:
<?php/* Global Non-namespace code */namespace index\name{ class foo{ function __construct () { echo 2; }} namespace{ $a = "index\\name\\foo";//escaped with \ So it can run normally, but if escape is omitted, the class ' Index\nameoo ' is given an error because the/F is interpreted as a page break $obj = New $a; }
This part of the space is temporarily here, the next chapter summarizes the use of NAMESPACE and __namespace__ in the namespace, and the use of aliases.