PHP Advanced Programming Learning Note 2014.06.12
Namespaces Overview
PHP supports namespaces at the beginning of the 5.3.0 version. What is a namespace? In a broad sense, namespaces are a way of encapsulating things. This abstract concept can be seen in many places. In PHP, namespaces are used to solve two types of problems encountered when writing a class library or application to create reusable code such as classes or functions:
The PHP namespace provides a way to group related classes, functions, and constants together. The following is an example of a PHP namespace syntax:
Define namespaces
Although any valid PHP code can be included in a namespace, only three types of code are affected by the namespace, which 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 language features in PHP, the same namespace can be defined in multiple files, allowing the content of the same namespace to be split into separate files. Of course you can also define multiple namespaces in the same file.
namespace MyProject; class myclass{ #code ...}
Define child namespaces: 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:
namespace myproject\helper\http; class myclass{ #code ...}
define multiple namespaces in the same file: There are two ways to declare multiple namespaces in the same file, but in practical programming practice, it is not recommended to define a multi-GE namespace in the same file. This approach is primarily 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, it is strongly not recommended to use this method, you can refer to the following brace definition method:
namespace myproject\helper\http; { class MyClass { #code ... }} namespace myproject\helper\request; { class MyClass { #code ... }}
Elements in the PHP namespace use
Before discussing how to use namespaces, you must understand how PHP knows which namespace elements to use. The class name can be referenced in three ways:
Using namespaces: Aliases/imports
Allowing the external fully qualified name to be referenced or imported through an alias is an important feature of the namespace. The PHP namespace supports two ways to use aliases or imports: Use aliases for class names, or use aliases for namespace names. In PHP, aliases are implemented using the operator use.
Note that PHP does not support importing functions or constants.
namespace as another; // The following example is the same as the use My\full\nsname as Nsname Use my\full\nsname; // Import a global class use \arrayobject;
Name resolution rules
Before we describe the name resolution rules, we'll look at some important definitions:
Name resolution follows these rules:
Example Name Resolution Example
Phpnamespace A; UseB\d, C\e asF;//function Callfoo (); //first try calling the function foo () defined in namespace "A"//and then try calling the global function "foo"\foo (); //Call global space function "foo"My\foo (); //call definition in namespace "a\my" in function "foo"F (); //first try calling the function "F" defined in Namespace "a"//and then try calling the global function "F"//class referenceNewB ();//create an object of class ' B ' defined in namespace ' a '///If not found, try to automatically load class "a\b"NewD ();//create an object of class ' D ' defined in namespace ' B ' using import rules//If not found, attempt to automatically load class "B\d"NewF ();//create an object of class ' E ' defined in namespace ' C ' using import rules//If not found, attempt to automatically load class "C\e"New\b ();//create an object that defines the class "B" in the global space//If it is not found, try to automatically load class "B"New\d ();//create an object that defines the class "D" in the global space//If it is not found, try to automatically load class "D"New\f ();//create an object that defines the class "F" in the global space//If it is not found, try to automatically load the class "F"//Call a static method or namespace function in another namespaceB\foo (); //Call namespace "a\b" in function "foo"B:: foo ();//Call the "Foo" Method of Class "B" defined in the namespace "A"//If the class "a\b" is not found, try to automatically load class "a\b"D:: foo ();//using the import rule, call the "Foo" Method of Class "D" defined in Namespace "B"///If the class "b\d" is not found, try to automatically load class "B\d"\b\foo (); //Call the function "foo" in the Namespace "B"\b:: foo ();//Call the "Foo" Method of Class "B" in global space//If class "B" is not found, try to automatically load static methods or functions in class "B"//Current namespacea\b:: foo ();//Call the "Foo" Method of Class "B" defined in the namespace "a\a"///If the class "a\a\b" is not found, try to automatically load class "a\a\b"\a\b:: foo ();//Call the "Foo" Method of Class "B" defined in the namespace "a\b"///If the class "a\b" is not found, try to automatically load class "a\b"?>
http://www.bkjia.com/PHPjc/781919.html www.bkjia.com true http://www.bkjia.com/PHPjc/781919.html techarticle PHP Advanced Programming Learning Note 2014.06.12 namespace overview PHP supports namespaces at the beginning of the 5.3.0 version. What is a namespace? In a broad sense, a namespace is a ...