Basic knowledge and memory for namespace learning in php

Source: Internet
Author: User
Tags constant php language strlen


Namespace overview
What is a namespace? In a broad sense, namespace is a way to encapsulate things. This abstract concept can be seen in many places. For example, a directory in the operating system is used to group related files. For files in the directory, it plays the role of namespace. For example, the file foo.txt can exist in the/home/greg and/home/other directories at the same time, but there cannot be two foo.txt files in the same directory. In addition, when accessing the foo.txt file outside the/home/greg Directory, we must put the directory name and directory separator before the file name to get/home/greg/foo.txt. This principle is applied to the field of programming as a namespace concept.
 
In PHP, the namespace is used to solve the two problems encountered when you create reusable code such as classes or functions when writing class libraries or applications:
 
1. The user-written code conflicts with the names of PHP internal classes/functions/constants or third-party classes/functions/constants.
2. Create an alias (or short) name for a long identifier name (usually defined to mitigate the first type of problem) to improve the readability of the source code.
 
The PHP namespace provides a way to combine related classes, functions, and constants. The following is an example of PHP namespace syntax:
 
<? Php
Namespace my \ name; // define the namespace
Class MyClass {}
Function myfunction (){}
Const MYCONST = 1;
$ A = new MyClass;
$ C = new \ my \ name \ MyClass; // global space
$ A = strlen ('hi'); // use namespace: Reserve global functions/constants
$ D = namespace \ MYCONST; // namespace operator and _ NAMESPACE _ constant
$ D = _ NAMESPACE _. '\ MYCONST ';
Echo constant ($ d); // namespace and dynamic language features
?>
PHP versions later than 5.3.0 support namespaces.
--------------------------------------------------------------------------------
Define a namespace
     
Although any valid PHP code can be included in the namespace, only three types of code are affected by the namespace. They are classes, functions, and constants.
 
The namespace is declared by the keyword namespace. If a file contains a namespace, it must declare the namespace before all other code.
     
<? Php
Namespace MyProject;
 
Const CONNECT_ OK = 1;
Class Connection {/*...*/}
Function connect (){/*...*/}
?>
The only valid code before declaring a namespace is the declare statement used to define the source file encoding method. In addition, all non-PHP code, including blank characters, cannot appear before the namespace declaration:
     
<Html>
<? Php
Namespace MyProject; // fatal error-the namespace must be the first statement of the program script
?>
In addition, unlike other PHP language features, the same namespace can be defined in multiple files, that is, the content of the same namespace can be separated and stored in different files.
--------------------------------------------------------------------------------
Define a sub-namespace
It has a similar relationship with directories and files. The PHP namespace also allows you to specify a hierarchical namespace name. Therefore, namespace names can be defined in different layers:
 
<? Php
Namespace MyProject \ Sub \ Level;
Const CONNECT_ OK = 1;
Class Connection {/*...*/}
Function connect (){/*...*/}
?>
The above example creates the constant MyProject \ Sub \ Level \ CONNECT_ OK, class MyProject \ Sub \ Level \ Connection and function MyProject \ Sub \ Level \ Connection.
--------------------------------------------------------------------------------
Define multiple namespaces in the same file
You can also define multiple namespaces in the same file. Defining multiple namespaces in the same file has two syntax forms.
 
<? Php
Namespace MyProject;
Const CONNECT_ OK = 1;
Class Connection {/*...*/}
Function connect (){/*...*/}
Namespace AnotherProject;
Const CONNECT_ OK = 1;
Class Connection {/*...*/}
Function connect (){/*...*/}
?>
 
We do not recommend that you use this syntax to define multiple namespaces in a single file. We recommend that you use the following syntax in braces.
 
<? Php
Namespace MyProject {
Const CONNECT_ OK = 1;
Class Connection {/*...*/}
Function connect (){/*...*/}
}
Namespace AnotherProject {
Const CONNECT_ OK = 1;
Class Connection {/*...*/}
Function connect (){/*...*/}
}
?>
 
In actual programming practices, it is not recommended to define multiple namespaces in the same file. This method is mainly used to merge multiple PHP scripts into the same file.
 
The code in a global non-namespace is combined with the code in the namespace. You can only use the syntax in braces. The global code must be enclosed by a namespace statement without a name. For example:
 
<? Php
Namespace MyProject {
Const CONNECT_ OK = 1;
Class Connection {/*...*/}
Function connect (){/*...*/}
}
Namespace {// global code
Session_start ();
$ A = MyProject \ connect ();
Echo MyProject \ Connection: start ();
}
?>
 
Except for the declare statement, no PHP code exists in the namespace brackets.
 
<? Php
Declare (encoding = 'utf-8 ');
Namespace MyProject {
Const CONNECT_ OK = 1;
Class Connection {/*...*/}
Function connect (){/*...*/}
}
 
Namespace {// global code
Session_start ();
$ A = MyProject \ connect ();
Echo MyProject \ Connection: start ();
}
?>
--------------------------------------------------------------------------------
Use namespace: Basic
Before discussing how to use a namespace, you must understand how PHP knows the elements in the namespace to be used. A simple analogy can be made between the PHP namespace and the file system. There are three methods to access a file in a file system:
 
1. The relative file name is in the format of foo.txt. It will be parsed to currentdirectory/foo.txt, where currentdirectory indicates the current directory. Therefore, if the current directory is/home/foo, the file name is resolved to/home/foo/foo.txt.
2. The relative path name format is subdirectory/foo.txt. It will be parsed as currentdirectory/subdirectory/foo.txt.
3. The absolute path name format is/main/foo.txt. It will be parsed to/main/foo.txt.
Elements in the PHP namespace use the same principle. For example, a class name can be referenced in three ways:
1. a non-qualified name or a class name without a prefix, for example, $ a = new foo (); or foo: staticmethod ();. If the current namespace is currentnamespace, foo will be parsed as currentnamespace \ foo. If the code using foo is global and is not included in any namespace, foo will be parsed as foo. Warning if the function or constant in the namespace is not defined, the undefined function name or constant name will be resolved to the global function name or constant name. For more information, see use namespace: backup global function name/constant name.
2. a qualified name or prefix name, for example, $ a = new subnamespace \ foo (); or subnamespace \ foo: staticmethod ();. If the current namespace is currentnamespace, foo will be parsed as currentnamespace \ subnamespace \ foo. If the code using foo is global and is not included in any namespace, foo will be parsed as subnamespace \ foo.
3. Fully qualified names, or names that contain Global prefix operators, such as $ a = new \ currentnamespace \ foo (); or \ currentnamespace \ foo: staticmethod ();. In this case, foo is always parsed as the text name (literal name) currentnamespace \ foo in the code.
 
The following is an example using these three methods:
 
File1.php
 
<? Php
Namespace Foo \ Bar \ subnamespace;
Const FOO = 1;
Function foo (){}
Class foo
{
Static function staticmethod (){}
}
?>
File2.php
 
<? Php
Namespace Foo \ Bar;
Include 'file1. Php ';
Const FOO = 2;
Function foo (){}
Class foo
{
Static function staticmethod (){}
}
/* Undefined name */
Foo (); // Parse to Foo \ Bar \ foo resolves to function Foo \ Bar \ foo
Foo: staticmethod (); // The static method resolved to class Foo \ Bar \ foo. Resolves to class Foo \ Bar \ foo, method staticmethod
Echo FOO; // resolves to constant Foo \ Bar \ FOO
 
/* Name limit */
Subnamespace \ foo (); // Resolves to function Foo \ Bar \ subnamespace \ foo
Subnamespace \ foo: staticmethod (); // Resolves to class Foo \ Bar \ subnamespace \ foo,
// And the class method staticmethod
Echo subnamespace \ FOO; // resolved to constant Foo \ Bar \ subnamespace \ FOO
 
/* Fully qualified name */
\ Foo \ Bar \ foo (); // Resolves to function Foo \ Bar \ foo
\ Foo \ Bar \ foo: staticmethod (); // Resolves to class Foo \ Bar \ foo and the class method staticmethod
Echo \ Foo \ Bar \ FOO; // The constant Foo \ Bar \ FOO is parsed.
?>
 
Note that you can use a fully qualified name to access any global class, function, or constant, such as \ strlen (), \ Exception, or \ INI_ALL.
 
<? Php
Namespace Foo;
 
Function strlen (){}
Const INI_ALL = 3;
Class Exception {}
 
$ A = \ strlen ('hi'); // call the global function strlen
$ B = \ INI_ALL; // access the global constant INI_ALL
$ C = new \ Exception ('error'); // instantiate a global class Exception
?>
--------------------------------------------------------------------------------
Namespaces and dynamic language features
The implementation of PHP namespaces is affected by the dynamic features of the language itself. Therefore, to convert the following code to a namespace:
 
Example1.php:
 
<? Php
Class classname
{
Function _ construct ()
{
Echo _ METHOD __, "\ n ";
}
}
Function funcname ()
{
Echo _ FUNCTION __, "\ n ";
}
Const constname = "global ";
 
$ A = 'classname ';
$ Obj = new $ a; // prints classname: :__ construct
$ B = 'funcname ';
$ B (); // prints funcname
Echo constant ('confame'), "\ n"; // prints global
?>
You must use a fully qualified name (including the class name with the namespace prefix ). Note that in the dynamic class name, function name, or constant name, the qualified name and fully qualified name are no different, so the leading backslash is unnecessary.
 
<? Php
Namespace namespacename;
Class classname
{
Function _ construct ()
{
Echo _ METHOD __, "\ n ";
}
}
Function funcname ()
{
Echo _ FUNCTION __, "\ n ";
}
Const constname = "namespaced ";
 
Include 'example1. Php ';
 
$ A = 'classname ';
$ Obj = new $ a; // prints classname: :__ construct
$ B = 'funcname ';
$ B (); // prints funcname
Echo constant ('confame'), "\ n"; // prints global
 
/* Note that if using double quotes, "\ namespacename \ classname" must be used */
$ A = '\ namespacename \ classname ';
$ Obj = new $ a; // prints namespacename \ classname ::__ construct
$ A = 'namespacename \ classname ';
$ Obj = new $ a; // also prints namespacename \ classname ::__ construct
$ B = 'namespacename \ funcname ';
$ B (); // prints namespacename \ funcname
$ B = '\ namespacename \ funcname ';
$ B (); // also prints namespacename \ funcname
Echo constant ('\ namespacename \ constname'), "\ n"; // prints namespaced
Echo constant ('namespacename \ constname'), "\ n"; // also prints namespaced
?>
--------------------------------------------------------------------------------
Namespace keyword and _ NAMESPACE _ constant
PHP supports two abstract methods to access the internal elements of the current NAMESPACE, __namespace _ magic constant and namespace keyword.
 
The constant _ NAMESPACE _ value is a string containing the name of the current NAMESPACE. Globally, code that is not included in any namespace contains an empty string.
 
<? Php
Namespace MyProject;
 
Echo '"', _ NAMESPACE __, '"'; // output "MyProject"
?>
Example #2 _ NAMESPACE _ Example, global code
 
<? Php
 
Echo '"', _ NAMESPACE __, '"'; // output ""
?>
Constant _ NAMESPACE _ is useful when a name is dynamically created, for example:
 
<? Php
Namespace MyProject;
 
Function get ($ classname)
{
$ A = _ NAMESPACE _. '\'. $ classname;
Return new $;
}
?>
 
The keyword namespace can be used to explicitly access elements in the current namespace or sub-namespace. It is equivalent to the self operator in the class.
 
<? Php
Namespace MyProject;
 
Use blah \ blah as mine; // see "Using namespaces: importing/aliasing"
 
Blah \ mine (); // callfunction blah \ mine ()
Namespace \ blah \ mine (); // callfunction MyProject \ blah \ mine ()
 
Namespace \ func (); // callfunction MyProject \ func ()
Namespace \ sub \ func (); // callfunction MyProject \ sub \ func ()
Namespace \ cname: method (); // callstatic method "method" of class MyProject \ cname
$ A = new namespace \ sub \ cname (); // instantiates object of class MyProject \ sub \ cname
$ B = namespace \ CONSTANT; // assigns value of constant MyProject \ CONSTANT to $ B
?>
 
<? Php
 
Namespace \ func (); // callfunction func ()
Namespace \ sub \ func (); // callfunction sub \ func ()
Namespace \ cname: method (); // callstatic method "method" of class cname
$ A = new namespace \ sub \ cname (); // instantiates object of class sub \ cname
$ B = namespace \ CONSTANT; // assigns value of constant CONSTANT to $ B
?>
--------------------------------------------------------------------------------
Use namespace: Alias/import
Allowing alias reference or external fully qualified names is an important feature of a namespace. This is similar to creating symbolic connections to other files or directories in unix-like file systems.
 
The PHP namespace supports two alias or import methods: use an alias for the class name or use an alias for the namespace name. Note that PHP does not support importing functions or constants.
 
In PHP, aliases are implemented through the use operator. Below is an example of using all three possible import methods:
 
<? Php
Namespace foo;
Use My \ Full \ Classname as Another;
 
// The following example is the same as use My \ Full \ NSname as NSname
Use My \ Full \ NSname;
 
// Import a global class
Use \ ArrayObject;
 
$ Obj = new namespace \ Another; // instantiate the foo \ Another object
$ Obj = new Another; // instantiate the My \ Full \ Classname object
NSname \ subns \ func (); // call the function My \ Full \ NSname \ subns \ func
$ A = new ArrayObject (array (1); // instantiate an ArrayObject object
// If "use \ ArrayObject" is not used, a foo \ ArrayObject object is instantiated.
?>
Note: For the name in the namespace (the fully qualified name that contains the namespace separator, such as Foo \ Bar, and the global name that does not contain the namespace separator, such as FooBar, the leading backslash is unnecessary or not allowed, because the imported name must be fully qualified and will not be parsed relative to the current namespace. Note that for namespaced names (fully qualified namespace names containing namespace separator, such as Foo \ Bar as opposed to global names that do not, such as FooBar ), the leading backslash is unnecessary and not allowed, as import names must be fully qualified, and are not processed relative to the current namespace.
 
To simplify the operation, PHP also supports multiple use statements in one line.
 
<? Php
Use My \ Full \ Classname as Another, My \ Full \ NSname;
 
$ Obj = new Another; // instantiate the My \ Full \ Classname object
NSname \ subns \ func (); // call the function My \ Full \ NSname \ subns \ func
?>
 
The import operation is compiled and executed, but the dynamic class name, function name, or constant name is not. Importing is saved med at compile-time, and so does not affect dynamic class, function or constant names.
 
<? Php
Use My \ Full \ Classname as Another, My \ Full \ NSname;
 
$ Obj = new Another; // instantiate a My \ Full \ Classname object
$ A = 'another ';
$ Obj = new $ a; // compile an Another object
?>
 
In addition, the import operation only affects non-qualified names and qualified names. The fully qualified name is determined, so it is not affected by the import.
 
<? Php
Use My \ Full \ Classname as Another, My \ Full \ NSname;
 
$ Obj = new Another; // instantiates object of class My \ Full \ Classname
$ Obj = new \ Another; // instantiates object of class Another
$ Obj = new Another \ thing; // instantiates object of class My \ Full \ Classname \ thing
$ Obj = new \ Another \ thing; // instantiates object of class Another \ thing
?>
--------------------------------------------------------------------------------
Global Space
If no namespace is defined, all classes and functions are defined in the global space, just as before the introduction of the namespace concept in PHP. Prefix \ is added before the name to indicate that the name is in the global space, even if the name is in another namespace.
 
<? Php
Namespace A \ B \ C;
 
/* This function is A \ B \ C \ fopen */
Function fopen (){
/*...*/
$ F = \ fopen (...); // call the global fopen function
Return $ f;
}
?>
--------------------------------------------------------------------------------
Use namespace: Reserve global functions/constants
In a namespace, when PHP encounters an unqualified class, function, or constant name, it uses different priority policies to parse the name. The class name is always resolved to the name in the current namespace. Therefore, you must use a fully qualified name to access the class name in the system or that is not included in the namespace. For example:
 
<? Php
Namespace A \ B \ C;
Class Exception extends \ Exception {}
 
$ A = new Exception ('hi'); // $ a is an object of Class A \ B \ C \ Exception
$ B = new \ Exception ('hi'); // $ B is an object of the Exception class.
 
$ C = new ArrayObject; // fatal error. The A \ B \ C \ ArrayObject class cannot be found.
?>
 
For functions and constants, if this function or constant does not exist in the current namespace, PHP will return and use the function or constant in the global space. For functions and constants, PHP will fall back to global functions or constants if a namespaced function or constant does not exist.
 
<? Php
Namespace A \ B \ C;
 
Const E_ERROR = 45;
Function strlen ($ str)
{
Return \ strlen ($ str)-1;
}
 
Echo E_ERROR, "\ n"; // output "45"
Echo INI_ALL, "\ n"; // output "7"-use the global constant INI_ALL
 
Echo strlen ('hi'), "\ n"; // output "1"
If (is_array ('hi') {// output "is not array"
Echo "is array \ n ";
} Else {
Echo "is not array \ n ";
}
?>
--------------------------------------------------------------------------------
Name resolution rules
Before describing the name resolution rules, let's take a look at some important definitions:
 
Namespace name definition
Unqualified name
The name does not contain the namespace separator identifier, such as Foo.
 
Qualified name
The name contains the namespace separator identifier, such as Foo \ Bar
 
Fully qualified name
The name contains the namespace separator and the identifier starting with the namespace separator, for example, \ Foo \ Bar. Namespace \ Foo is also a fully qualified name.
 
 
Name resolution follows the following rules:
 
1. Calls to fully qualified functions, classes, and constants are parsed during compilation. For example, new \ A \ B is parsed as Class A \ B.
2. All non-qualified names and qualified names (non-fully qualified names) are converted during compilation according to the current import rules. For example, if the namespace A \ B \ C is imported as C () is converted to A \ B \ C \ D \ e ().
3. In the namespace, all qualified names that are not converted according to the import rules will be prefixed with the current namespace name. For example, if C \ D \ e () is called in namespace A \ B, C \ D \ e () is converted to A \ B \ C \ D \ e ().
4. Unqualified class names are converted during compilation according to the current import rules (use the full name instead of the short import name ). For example, if the namespace A \ B \ C is imported as C, new C () is converted to new A \ B \ C ().
5. Within the namespace (for example, A \ B), function calls with non-qualified names are parsed at runtime. For example, the call to function foo () is parsed as follows:
1. Find the function named A \ B \ foo () in the current namespace
2. Try to find and call the function foo () in the global space ().
6. Calls to non-qualified names or qualified name classes (non-fully qualified names) within the namespace (for example, A \ B) are resolved at runtime. The following is the parsing process of calling new C () and new D \ E (): new C:
1. Search for A \ B \ C class in the current namespace.
2. Try to automatically load class A \ B \ C.
New D \ E () parsing:
1. Add the current namespace name before the class name to A \ B \ D \ E, and then search for the class.
2. Try to automatically load class A \ B \ D \ E.
To reference global classes in a global namespace, you must use the fully qualified name new \ C ().
 
<? Php
Namespace;
Use B \ D, C \ E as F;
 
// Function call
 
Foo (); // first try to call the function foo () defined in namespace "()
// Try calling the global function "foo" again"
 
\ Foo (); // call the global space function "foo"
 
My \ foo (); // call the definition in the namespace "A \ my" function "foo"
 
F (); // first try to call the function "F" defined in namespace ""
// Try to call the global function "F" again"
 
// Class reference
 
New B (); // create an object of class "B" defined in namespace ""
// If not found, automatically load the class "A \ B"
 
New D (); // use the import rules to create an object of the class "D" defined in namespace "B"
// If not found, automatically load the class "B \ D"
 
New F (); // use import rules to create an object of class "E" defined in namespace "C"
// If not found, automatically load the class "C \ E"
 
New \ B (); // create an object of class "B" defined in the global space
// If not found, try to automatically load class "B"
 
New \ D (); // create an object of the class "D" defined in the global space
// If not found, try to automatically load class "D"
 
New \ F (); // create an object of the class "F" defined in the global space
// If not found, try to automatically load the class "F"
 
// Call a static method or namespace function in another namespace
 
B \ foo (); // call the "foo" function in the namespace "A \ B"
 
B: foo (); // call the "foo" method of class "B" defined in namespace ""
// If the class "A \ B" is not found, automatically load the class "A \ B"
 
D: foo (); // use the import rule to call the "foo" method of class "D" defined in namespace "B"
// If the class "B \ D" is not found, try to automatically load the class "B \ D"
 
\ B \ foo (); // call the function "foo" in namespace "B"
 
\ B: foo (); // call the "foo" method of class "B" in the global space
// If class "B" is not found, try to automatically load class "B"
 
// Static methods or functions in the current namespace
 
A \ B: foo (); // call the "foo" method of class "B" defined in namespace "A \"
// If the class "A \ B" is not found, try to automatically load the class "A \ B"
 
\ A \ B: foo (); // call the "foo" method of class "B" defined in namespace "A \ B"
// If the class "A \ B" is not found, automatically load the class "A \ B"
?>

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.