Home > Backend Development > PHP Tutorial > PHP object-oriented programming (oop) study notes (5) - PHP namespace_PHP tutorial

PHP object-oriented programming (oop) study notes (5) - PHP namespace_PHP tutorial

WBOY
Release: 2016-07-13 10:28:30
Original
896 people have browsed it

Namespace Overview

In PHP, namespaces are used to solve two types of problems encountered when creating reusable code such as classes or functions when writing class libraries or applications:

Name conflicts between user-written code and PHP internal classes/functions/constants or third-party classes/functions/constants.
Create an alias (or short) name for a very long identifier name (usually defined to alleviate the first type of problem), improving the readability of the source code.
PHP namespaces provide a way to group related classes, functions, and constants together. Here is an example illustrating PHP namespace syntax:

Define namespace

Although any legal PHP code can be included in a namespace, there are only three types of code affected by namespaces: classes, functions, and constants. Namespaces are declared using the keyword namespace. If a file contains a namespace, it must declare the namespace before all other code. In addition, unlike other language features of PHP, the same namespace can be defined in multiple files, which allows the contents of the same namespace to be divided and stored in different files. Of course you can also define multiple namespaces in the same file.

Copy code The code is as follows:

namespace MyProject;
class MyClass
{
#code. ..
}

Define sub-namespaces: Much like the relationship between directories and files, PHP namespaces also allow you to specify hierarchical namespace names. Therefore, namespace names can be defined in a hierarchical manner:

Copy code The code is as follows:

namespace MyProjecthelperhttp;
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 practice, it is highly discouraged to define the Dogo namespace in the same file. This method is mainly used to combine multiple PHP scripts in the same file. The first method is listed below.

Copy code The code is as follows:

namespace MyProjecthelperhttp;
class MyClass
{
  #code. ..
}
namespace MyProjecthelperrequest;
class MyClass
{
   #code...
}

However, it is strongly not recommended to use this method. You can refer to the following brace definition method:

Copy code The code is as follows:

namespace MyProjecthelperhttp;
{
class MyClass
{
 🎜>

Elements in the PHP namespace use

Before discussing how to use namespaces, you must understand how PHP knows which namespace elements to use. Class names can be referenced in three ways:

Unqualified name, or a class name without a prefix, such as $a=new foo(); or foo::staticmethod();. If the current namespace is currentnamespace, foo will be resolved to currentnamespacefoo. If the code using foo is global and does not contain code in any namespace, foo will be resolved as foo. Warning: If a function or constant in the namespace is undefined, the unqualified function or constant name is resolved to a global function or constant name. See Using Namespaces: Fallback Global Function Names/Constant Names for details.

Qualified name, or name including a prefix, such as $a = new subnamespacefoo(); or subnamespacefoo::staticmethod();. If the current namespace is currentnamespace, foo will be resolved to currentnamespacesubnamespacefoo. If the code using foo is global, code not contained in any namespace, foo will be resolved to subnamespacefoo.

Fully qualified name, or a name that includes a global prefix operator, for example, $a = new currentnamespacefoo(); or currentnamespacefoo::staticmethod();. In this case, foo is always resolved to the literal name currentnamespacefoo in the code.
Use namespace: alias/import

Allowing external fully qualified names to be referenced or imported via aliases is an important feature of namespaces. PHP namespace support There are two ways to use aliases or imports: using aliases for class names, or using aliases for namespace names. In PHP, aliases are implemented through the operator use.

Note that PHP does not support imported functions or constants.

Copy code The code is as follows:

namespace foo;
use MyFullClassname as Another;

// The following example is the same as use MyFullNSname as NSname
use MyFullNSname;

//Import a global class
use ArrayObject;

Name resolution rules

Before explaining the name resolution rules, let’s look at some important definitions:

Unqualified name: An identifier that does not contain a namespace delimiter in its name, such as Foo
Qualified name: An identifier that contains a namespace delimiter in its name, such as FooBar
Fully qualified name Fully qualified name: An identifier whose name contains a namespace delimiter and starts with a namespace delimiter, such as FooBar. namespaceFoo is also a fully qualified name.
Name resolution follows the following rules:

Calls to fully qualified names of functions, classes and constants are resolved at compile time. For example new AB resolves to class AB.
All unqualified names and qualified names (not fully qualified names) are converted at compile time according to the current import rules. For example, if namespace ABC is imported as C, then calls to CDe() are converted to ABCDe().
Within a namespace, all qualified names that are not converted according to import rules will have the current namespace name prepended to them. For example, if CDe() is called inside namespace AB, CDe() will be converted to ABCDe().
Unqualified class names are converted at compile time according to the current import rules (full names are used instead of short import names). For example, if namespace ABC is imported as C, new C() is converted to new ABC() .
Within a namespace (e.g. AB), function calls to unqualified names are resolved at runtime. For example, a call to function foo() is parsed as follows:
1) Find a function named ABfoo() in the current namespace
2) Try to find and call the function foo() in the global space ).
Calls to unqualified names or qualified name classes (non-fully qualified names) inside a namespace (e.g. AB) are resolved at runtime. The following is the parsing process of calling new C() and new DE(): Parsing of new C():
Find the ABC class in the current namespace.
Try to autoload class ABC.

Parsing of new DE():
Add the current namespace name in front of the class name to become: ABDE, and then search for the class.
Try autoloading class ABDE.

In order to reference a global class in the global namespace, the fully qualified name new C() must be used.

Example name resolution example

Copy code The code is as follows:

namespace A;
use BD, CE as F;
// Function call
foo(); // First try to call the one defined in the namespace" Function foo()
in A" // Try to call the global function "foo"
foo(); // Call the global function "foo"
myfoo(); // The call is defined in the naming Function "foo" in space "Amy"
F(); // First try to call function "F" defined in namespace "A"
// Then try to call global function "F"
// Class reference
new B(); // Create an object of class "B" defined in namespace "A"
// If not found, try to automatically load class "AB"
new D(); // Use import rules to create an object of class "D" defined in namespace "B"
); // Use the import rules to create an object of "E" defined in the naming space "E" defined
// If not found, try to automatically load the class "CE"
New B (); / /Create an object of class "B" defined in the global space
                                             '' // ‐ ‐ //       ‐ ‐ ‐‐‐‐‐‐‐‐‐ ​​ ​​An object of "D"
// If not found, try to automatically load class "D"
new F(); // Create an object of class "F" defined in the global space
// If not found, try to automatically load class "F"
// Call a static method or namespace function in another namespace
Bfoo(); // Call a function in namespace "AB"" foo"
B::foo(); // Call the "foo" method
of class "B" defined in namespace "A" // If class "AB" is not found, try to automatically load the class "AB"
D::foo(); // Use import rules to call the "foo" method
of class "D" defined in namespace "B" // If class "BD" is not found, Then try to automatically load the class "BD"
Bfoo(); // Call the function "foo" in the namespace "B"
B::foo(); // Call the class "B" in the global space "foo" method
// If class "B" is not found, try to automatically load class "B"
// Static method or function in the current namespace
AB::foo(); // Call the "foo" method
of class "B" defined in namespace "AA" // If class "AAB" is not found, try to automatically load class "AAB"
AB::foo() ; // Call the "foo" method
of class "B" defined in namespace "AB" // If class "AB" is not found, try to automatically load class "AB"
?>


http://www.bkjia.com/PHPjc/788610.html

truehttp: //www.bkjia.com/PHPjc/788610.htmlTechArticleNamespace Overview In PHP, namespaces are used to solve the problem of creating reusable objects when writing class libraries or applications. Two types of problems encountered when the code is such as a class or function: User-written code and P...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template