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 key word 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 promises to have a colon, as long as it is not the first character and the 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) file://uppercase
{
Return (Strtoupper ($text));
}
}
Make Non-oo interface to build a non-OO interface
function uppercase ($text)
{
$e = new Textengine;
Return ($e->uppercase ($text));
}
}
Test class in namespace testing the classes in the namespace
$e = new Core_php:utility::textengine;
Print ($e->uppercase ("from Object"). "
");
Test function in namespace testing the functions in the namespace
Print (Core_php:utility::uppercase ("from function"). "
");
Bring class into global namespace to import classes into a globally named space
Import class textengine from core_php:utility;
$e 2 = new Textengine;
?>
The import statement imports a part of the namespace into the global namespace.
To import a single member of a namespace, you can specify a type of Constant,function or class, followed by the name of the member;
such as import class XXX
If you want to import all the members of a particular type, you can use * instead of the name;
Import constant * Imports all constants
If you want to import all members of all types, use *.
such as import *
After the member, add the name of the namespace with the from key.
such as import class Textengine from core_php:utility;
In short you have to write a statement like import * from MyNamespace or import class Textengine from Core_php:utility, as in example 6.17.
http://www.bkjia.com/PHPjc/631947.html www.bkjia.com true http://www.bkjia.com/PHPjc/631947.html techarticle 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 somewhere else. In a short script, the first ...