Namespace. It is quite difficult to name variables, functions, and classes. in addition to taking into account the ease of understanding of the variable name, you also need to worry about whether the name has been used elsewhere. in a short script, it is quite difficult to name variables, functions, and classes. besides the variable names, you need to worry about whether the name has been used somewhere else. in a short script, the second problem is the basic problem. when you consider reusing your code, the project code after this must avoid using the names you have used. generally, reusable code is always included in functions or classes, and many possible naming conflicts need to be handled. however, a name conflict may occur between a function and a class. you can try to avoid this situation by adding a prefix to all classes, or you can use the namespace statement.
The Namespace keyword is used to name a piece of code. outside the code block, the script must use the operator: add the namespace name to reference the code block. the same method is used to reference static class members. the code in the namespace does not need to declare the namespace. it is the default one. this method is better than adding a prefix. your code can be more compact and readable.
You may want to know whether you can create a hierarchical (nested) namespace. the answer is No. however, you can add a colon after the namespace name. you can call a variable, function, and class that does not contain a colon in the name again. the namespace promises to have a colon, as long as it is not the first character and the last character or followed by another colon. the colon in the namespace name does not make any sense for PHP, but if you use them to distinguish logical blocks, they can well describe the parent and child in your code) link.
/* Note: You can use this method:
Namespace animal: dog {}
Namespace animal: pig {}
Use a colon to describe the parent-child relationship.
*/
You may not include any function, class, or constant definition in a namespace statement. this will prevent you from using them to improve the old function library that uses global variables. the namespace is most suitable for object orientation. constants in a namespace use the same syntax as constants in a class.
Example 6.17 shows how to use a namespace.
Listing 6.17 Using a namespace
Namespace core_php: utility
{
Class textEngine
{
Public function uppercase ($ text) file: // uppercase
{
Return (strtoupper ($ text ));
}
}
// Make non-OO interface creates a non-OO interface
Function uppercase ($ text)
{
$ E = new textEngine;
Return ($ e-> uppercase ($ text ));
}
}
// Test class in namespace test the class in the namespace
$ E = new core_php: utility: textEngine;
Print ($ e-> uppercase ("from object ")."
");
// Test function in namespace test the function in the namespace
Print (core_php: utility: uppercase ("from function ")."
");
// Bring class into global namespace import class to global namespace
Import class textEngine from core_php: utility;
$ E2 = new textEngine;
?>
The Import statement imports a part of the namespace into the global namespace.
To import a single namespace member, you can specify the type as constant, function, or class, and then write the member name;
// For example, import class XXX
If you want to import all members of a specific type, you can use * instead of the name;
// For example, import constant * to import all constants
If you want to import all types of members, use.
// For example, import *
Add the namespace name with the from keyword after the member.
// For example, import class textEngine from core_php: utility;
In short, you need to write a statement like import * from myNamespace or import class textEngine from core_php: utility, as in example 6.17.
In a short script...