Naming variables, functions and classes is difficult. In addition to taking into account the variable's name to be easy to understand, you also have 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 that must avoid the name you used. Generally speaking, reusable code is always included in a function or class and needs to deal with many possible naming conflicts. But life name conflicts can occur between functions and classes. You can try to avoid this by prefixing all classes, or you can use the namespace statement.
The Namespace keyword gives a piece of code. Outside this code block, the script must refer to the code block with the operator:: plus the namespace name. The same method is used to refer to static class members. The code does not need to declare the namespace in the namespace, it is the default itself. This method is better than adding a prefix. Your code can thus become more compact and readable.
You may be wondering if it is possible to create a hierarchical (necked) namespace. The answer is no. But you can add a colon after the namespace name, and you can call variables, functions and classes that do not contain colons in the name. The namespace allows colons to exist as long as they are not the first and last characters or followed by another colon. The colons in the namespace have no meaning to PHP, but if you use them to distinguish logical blocks, they can make a good illustration of the parent-child relationship in your code.
/* Note: This is possible:
namespace animal:dog {}
namespace animal:pig {}
Use colons to illustrate parent-child relationship.
*/
You may not include anything other than a function, class, or constant definition within a namespace statement. This will prevent you from using them to improve the old library of function that uses global variables. Namespaces are best suited for object-oriented. Constants within a namespace use the same syntax as constants in a class.
Example 6.17 shows how to use namespaces.
Listing 6.17 Using a namespace
<?php
namespace core_php:utility
{
class textEngine
{
public function uppercase($text) file://caps
{
return(strtoupper($text));
}
}
//make non-OO interface Create a non-OO interface
function uppercase($text)
{
$e = new textEngine;
return($e->uppercase($text));
}
}
//test class in namespace tests classes in namespace
$e = new core_php:utility::textEngine;
print($e->uppercase("from object") . "<br>");
//test function in namespace Test function in namespace
print(core_php:utility::uppercase("from function") . "<br>");
//bring class into global namespace Import the class into 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 member of a single namespace, you can specify the type as constant, function or class, and then write the name of the member;
//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 * import all constants
If you want to import all members of all types, use *.
//For example, import *
After the member, add the name of the namespace with the from keyword.
//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, just like in Example 6.17.