PHP object-oriented programming (oop) learning notes (5)-PHP namespace _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP object-oriented programming (oop) learning notes (5)-PHP namespace. Namespace overview in PHP, namespace is used to solve two problems encountered when you create reusable code such as classes or functions when writing class libraries or applications: user-written code and P Namespace overview

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 user-written code conflicts with the names of PHP internal classes/functions/constants or third-party classes/functions/constants.
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:

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.

The code is as follows:


Namespace MyProject;
Class MyClass
{
# Code...
}

Define a sub-namespace: it has a very 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:

The code is as follows:


Namespace MyProject \ helper \ http;
Class MyClass
{
# Code...
}

Defining multiple namespaces in the same file: There are two ways to declare multiple namespaces in the same file. However, in actual programming practices, it is strongly 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.

The code is as follows:


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:

The code is as follows:


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:

A non-qualified name or a class name that does not contain 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.

A qualified name, or a name containing a prefix, such as $ 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.

A fully qualified name or a name that contains the global prefix operator, for example, $ 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.
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.

The code is as follows:


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;

Name resolution rules

Before describing the name resolution rules, let's take a look at some important definitions:

Unqualified name: the name does not contain the namespace separator identifier, for example, 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:

Calls to fully qualified functions, classes, and constants are parsed during compilation. For example, new \ A \ B is parsed as Class A \ B.
All unqualified names and qualified names (not 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 ().
Within 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 ().
The unqualified class name is converted during compilation according to the current import rule (the short import name is replaced by the full name ). For example, if the namespace A \ B \ C is imported as C, new C () is converted to new A \ B \ C ().
In A 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 ().
Calls to A non-qualified name or qualified name class (not fully qualified name) within A 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:
Find the \ B \ C class in the current namespace.
Try to automatically load class A \ B \ C.

New D \ E () parsing:
Add the name of the namespace before the class name to A \ B \ D \ E, and then search for the class.
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 ().

Example

The code is as follows:


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"
?>

In PHP, namespaces are used to solve the two problems encountered when you create reusable code such as classes or functions when writing class libraries or applications: user-written code and P...

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.