Namespace namespace php5.3
1.1 Overview of namespaces
The application of this principle to the field of programming is the concept of namespaces.
The problem solved:
1. User-written code conflicts with the name of a class/function/constant or third-party class/function/constant within PHP.
2. Create an alias (or short) name for a very long identifier name (usually defined to alleviate the first type of problem) and improve the readability of the source code.
1.2 Define Namespaces
Format:
Namespace Space Name
Attention:
①namespace before any output including header
② if previously mentioned system conflict issues take precedence over the class function constants in your own namespace
③ introduces a file that is independent of the name of the namespace, the class function constants that must be connected with the access path and that do not have namespaces default to the root space
1.3 Defining a sub-namespace
Namespace lamp\php
Note: The path can be relative and absolute but cannot be used on a single drive. /
1.4 Defining multiple namespaces in the same file
namespace A;
namespace B;
Or
Namespace a{
}
Namespace b{
}
1.5 using namespaces
Unqualified name demo ();
Qualified name Php\demo ();
Fully qualified name \lamp\php\demo ();
1.6 Namespaces and dynamic language features
Constant function classes
Use a fully qualified name when using dynamic language special new
1.7 NAMESPACE keywords and __namespace__ Magic constants
Namespace defining a Namespace
namespace refers to the current namespace name
__NAMESPACE__ represents the current namespace name
1.8 Using namespaces: Aliases/imports
Use aliases replaced with namespace
Use namespace as alias//use LAMP\PHP\OOP as LP;
The use namespace defaults to the last-level space name as an alias
Use Import
Use space name \ class name;
1.9 Global Space
Default in global root space if no namespace
1.10 Using namespaces: Fallback global functions/constants
Function constants can be
Class is not available for
1.11 Name parsing rules
Object-oriented eighth day----namespaces