Naming variables, functions and classes is quite difficult. In addition to considering that the name of the variable should be easy to understand, you also have to worry about whether the name has been used somewhere else. In a small script, the first The two questions are fundamental. When you consider reusing your code, the code in subsequent projects must avoid using the names you have used. Generally speaking, reusable code is always contained in functions or classes, which requires a lot of processing Possible naming conflicts. But naming conflicts can also 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 names a block of code. Outside this block of code, scripts must reference this block of code using operator:: followed by the name of the namespace. The same method is used to reference static class members. Code within the namespace does not You need to declare the namespace, which is the default. This method is better than adding a prefix. Your code will become more compact and readable.
You may wonder whether it is possible to create hierarchical (Nested) namespaces. The answer is no. But you can add a colon after the namespace name, and you can call variables, functions and classes again that do not contain a colon in the name. Colons are allowed in namespaces as long as they are not The first and last characters or followed by another colon. Colons in namespace names don't mean anything to PHP, but if you use them to separate logical chunks, they can tell a good story about your The parent-child relationship in the code.
Note: You can use this:
namespace animal:dog {}namespace animal:pig {} Copy after login |
Use colons to illustrate the parent-child relationship.
You may not include anything other than function, class or constant definitions within a namespace statement. This will prevent you from using them to improve older function libraries that use global variables. Namespaces are best suited for Object. Constants within a namespace use the same syntax as constants within a class.
Example 1 shows how to use a namespace.
Example 1<?php namespace core_php:utility { class textEngine { public function uppercase($text) //大写 { return(strtoupper($text)); } } //make non-OO interface 建立一个非OO的接口 function uppercase($text) { $e = new textEngine; return($e->uppercase($text)); } } //test class in namespace 测试命名空间中的类 $e = new core_php:utility::textEngine; print($e->uppercase("from object") . "<br>"); //test function in namespace 测试命名空间中的函数 print(core_php:utility::uppercase("from function") . "<br>"); //bring class into global namespace 把类导入全局命名空间 import class textEngine from core_php:utility; $e2 = new textEngine; ?> Copy after login |
To import members of a single namespace, you can specify the type as constant, function or class, then write the name of the member;
//For example, import class XXXIf you want to import all members of a specific type, you can use * instead of the name ;//For example, import constant * to import all constantsIf you want to import all members of all types, just use *. //For example, import *After the member, use the from keyword plus the name of the namespace.//For example, import class textEngine from core_php:utility;In short, you have to write something like import * from myNamespace or import class textEngine from core_php:utility, as shown in Figure 1.