Section 14th-Namespaces
Naming variables, functions, and classes is difficult, except to take into account the name of the variable to be easy to understand, but also to worry about whether the name has been used elsewhere. In a short script, the second problem is the basic problem. When you consider reusing your code, after that the project code must avoid using the naming you have used. In general, reusable code is always included in a function or class, and there are many possible naming conflicts that need to be handled. However, a naming conflict can also occur between functions and classes. You can try to avoid this by adding prefixes before all classes, or you can use the namespace statement.
namespace keyword to name a piece of code. Outside this block of code, the script must refer to the code block with the operator:: plus the name of the namespace. Referencing static class Members is also used in the same way. In a namespace, code does not need to declare a namespace, which itself is the default. This method is better than the method of adding prefixes. Your code can become more compact and readable.
You might want to know if you can create a layered (nested) namespace. The answer is no. But you can add a colon to the namespace name, and you can call variables, functions, and classes that do not contain colons in the name. The namespace allows the presence of a colon, as long as it is not the first and last character or then another colon. The colon in the name of the namespace has no meaning for PHP, but if you use them to distinguish logical chunks, they can well describe the parent-child (parent-child) relationship in your code.
/* Note: You can use this:
Namespace Animal:dog {}
Namespace Animal:pig {}
Use a colon to describe the parent-child relationship.
*/
You might not have anything outside of a namespace statement that contains a function, class, or constant definition. This will prevent you from using them to improve the old library of functions that use global variables. Namespaces are best suited for object-oriented. Constants in a namespace use the same syntax as constants in a class.
Example 6.17 shows how namespaces are used.
Listing 6.17 Using a namespace
Namespace Core_php:utility
{
Class Textengine
{
Public function uppercase ($TEXT)//Uppercase
{
Return (Strtoupper ($text));
}
}
http://www.bkjia.com/PHPjc/445272.html www.bkjia.com true http://www.bkjia.com/PHPjc/445272.html techarticle the 14th section-namespace naming variables, functions and classes is difficult, except to take into account the name of the variable to be easy to understand, but also to worry about whether the name has been used elsewhere ...