Section 14 - Name Space - Classes and Objects in PHP5 [14]
Author: Leon Atkinson translation: Haohappy Source: Beyond the PHP / * ------------------------------------ ------------------------------------------ | = --- -------------------------------------------------- -------------------------- * / 14 - Namespace naming variables, functions and classes are difficult, except Considering the name of the variable is easy to understand, we must worry if this name has been used in some places. In a small script, the second problem is the basic problem. When you consider reusing your code, after this Project code must avoid using the name you have used. Usually, reusable code is always included in a function or class, you need to handle many might of naming conflicts. But there may be a naming conflict between functions and classes. You You can try to avoid this situation, by adding a prefix before all classes, or you can use the Namespace statement .NameSpace keyword to name the code. External, script must be used with an operator: Plus Namespace Name to reference this code block. Quote Static class member is also the same method. In the namespace code does not need to declare namespace, it itself is default. This method is better than the prefix method. Your code can be This becomes more compact and readable. You may want to know if you can build a hierarchical (nested) namespace. The answer is not. But you can add a colon after the namespace name, you can call in the name again Do not include the variables, functions, and classes of the colon. Namespaces allow the colon, as long as it is not the first character and the last character or the other colon. The name of the name is no meaning for PHP, but if You use them to distinguish the logical blocks, they can express the Parent-child relationship in your code. / * Note: You can use this: Namespace Animal: Dog {} Namespace Animal: Pig { } With the colon, you will explain the Parent-Child relationship. * / You may have no anything other than the function, class, or constant definition in a namespace statement. This will prevent you from using them to improve the library of global variables. The namespace is best suited to object-oriented. The constants within the namespace use the same syntax as the constant in the class. Example 6.17 shows how to use namespace. LiSting 6.17 Using A Namespace
Namespace core_php: utility {class textengine {public function uppercase ($ text) // uppercase {RETURN (STRTOUPPER ($ text));}}} // make Non-oo interface created a non-OO interface Function Uppercase ($ text) { $ E = new textengine; return ($ e-> Uppercase ($ text));}} // Test Class in Namespace Test Namespace Class $ E = New Core_php: Utility :: TeXtengine; Print ($ E-> Uppercase ("from object"). ""); // Test Function in Namespace Test the function print in the namespace (Core_php: Utility :: Uppercase ("from function"). ""); // Bring Class Into Global Namespace Import the class into the global namespace Import Class TextEngine from core_php: utility; $ E2 = new textengine;> import statement imports some part of the namespace into a global namespace. To import a single namespace member, you can specify Types are constant, function or class, then write the name of the member; // If you want to import a particular type of member, you can use * instead of name; // If import constant * import all constants If you want to import all of all types of members, use *, if import * After the member, use the from keyword plus namespace name. A statement like Import * from MyNamespace or Import Class TextEngine from core_php: UTILITY is like this. There is no namespace keyword in PHP5. The above example is the result is PARSE ERROR: PARSE ERROR, UNEXPECTED T_String in E: MyWebtestphp5.php on line 2
Originally in the development plan, later canceled in RC2 :(