PHP Advanced Programming Study Notes
Namespace Overview
PHP versions later than 5.3.0 support namespaces. What is a namespace? In a broad sense, namespace is a way to encapsulate things. This abstract concept can be seen in many places. 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:
The PHP namespace provides a way to combine related classes, functions, and constants. The following is an example of PHP namespace Syntax:
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. 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. Of course, you can also define multiple namespaces in the same file.
namespace MyProject;class MyClass{ #code...}
Define the 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:
namespace MyProject\helper\http;class MyClass{ #code...}
Define multiple namespaces in the same file:There are two methods to declare multiple namespaces in the same file. However, in actual programming practices, it is not recommended to define the dogo namespace in the same file. This method is mainly used to merge multiple PHP scripts into the same file. The first method is listed below.
namespace MyProject\helper\http;class MyClass{ #code...}namespace MyProject\helper\request;class MyClass{ #code...}
However, this method is strongly not recommended. You can refer to the following braces for definition:
namespace MyProject\helper\http;{ class MyClass { #code... }}namespace MyProject\helper\request;{ class MyClass { #code... }}
Use of elements in the PHP namespace
Before discussing how to use a namespace, you must understand how PHP knows the elements in the namespace to be used. Class names can be referenced in three ways:
Use namespace: alias/import
Allowing alias reference or external fully qualified names is an important feature of a namespace. The PHP namespace supports two alias or import methods: use an alias for the class name or use an alias for the namespace name. In PHP, aliases are implemented through the use operator.
Note that PHP does not support importing functions or constants.
Namespace foo; use My \ Full \ Classname as Another; // The example below is the same as use My \ Full \ NSname as NSname use My \ Full \ NSname; // import a global class use \ ArrayObject;
Name resolution rules
Before describing the name resolution rules, let's take a look at some important definitions:
Name resolution follows the following rules:
Example
<? Phpnamespace A; use B \ D, C \ E as F; // function call foo (); // first, try to call the function foo () defined in namespace "() // call the global function "foo" \ foo (); // call the global space function "foo" my \ foo (); // call the definition function "foo" F () in the namespace "A \ my (); // first try to call the function "F" defined in namespace "A" // then try to call the global function "F" // class reference new B (); // create an object of Class "B" defined in namespace "A" // if not found, automatically load class "A \ B" new D (); // use the import rules to create an object of the class "D" defined in namespace "B" // if not found, then try to automatically load the class "B \ D" new F (); // use the import rule, create an object of Class "E" defined in namespace "C" // if not found, try Test automatic loading class "C \ E" new \ B (); // create an object of Class "B" defined in the global space // if not found, then try to automatically load class "B" new \ D (); // create an object of Class "D" defined in the global space // if not found, then try to automatically load class "D" new \ F (); // create an object of Class "F" defined in the global space // if not found, then try to automatically load the class "F" // call the static method or namespace function B \ foo () in another namespace (); // call the function "foo" B: foo () in namespace "A \ B (); // call the "foo" method of Class "B" defined in namespace "A" // if the class "A \ B" is not found ", then try to automatically load the class "A \ B" D: foo (); // use the import rule, call the "foo" method of Class "D" defined in namespace "B" // if Class "B \ D" is not found, try to automatically load class "B \ D" "\ B \ foo (); // call the namespace" B "function" foo "\ B: foo (); // call the "foo" method of Class "B" in the global space // If class "B" is not found, then try to automatically load the static method or function A \ B: foo (); // call the "foo" method of Class "B" defined in namespace "A \ A" // If class "A \ B" is not found, then try to automatically load the class "A \ B" \ A \ B: foo (); // call the "foo" method of Class "B" defined in namespace "A \ B" // If class "A \ B" is not found, then try to automatically load the class "A \ B"?>