Section 14th -- namespace -- ClassesandObjectsinPHP514

Source: Internet
Author: User
/* + Tips + | this document is Haohappy-read | notes in the ClassesandObjects chapter | translation-oriented + personal experiences | do not repost the notes to avoid unnecessary troubles. thank you |

/* + --------------------------------------------------------------------------------- + | = This article is Haohappy read < > | = Notes in the middle Classes and Objects chapter | = Translation + personal experience | = do not repost to avoid unnecessary troubles, thank you | = Welcome to criticize and correct, hope to make progress together with all PHP enthusiasts! + ----------------------------------------------------------------------------- + */Section 14th -- namespace name variables, functions and classes are quite difficult. besides considering the name of variables, you need 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 this must avoid using the names you have used. generally, reusable code is always included in functions or classes, and many possible naming conflicts need to be handled. however, a name conflict may occur between a function and a class. you can try to avoid this situation by adding a prefix to all classes, or you can use the namespace statement. the Namespace keyword is used to name a piece of code. outside the code block, the script must use the operator: add the namespace name to reference the code block. the same method is used to reference static class members. the code in the namespace does not need to declare the namespace. it is the default one. this method is better than adding a prefix. your code can be more compact and readable. you may want to know whether you can create a hierarchical (nested) namespace. the answer is No. however, you can add a colon after the namespace name. you can call a variable, function, and class that does not contain a colon in the name again. the namespace allows the existence of colons, as long as it is not the first character and the last character or followed by another colon. the colon in the namespace name does not make any sense for PHP, but if you use them to distinguish logical blocks, they can well describe the parent and child in your code) link. /* Note: You can use namespace animal: dog {} namespace animal: pig {} to describe the parent-child relationship. */You may not include anything other than functions, classes, or constant definitions in a namespace statement. this will prevent you from using them to improve the old function library that uses global variables. the namespace is most suitable for object orientation. constants in a namespace use the same syntax as constants in a class. example 6.17 shows how to use a namespace. listing 6.17 Using a namespace 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 (core_php: utility: uppercase (" from function ") in the namespace ")."
"); // Bring class into global namespace import class to 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 single namespace member, you can specify the type as constant, function, or class, and then write the member name; // 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 * to import all constants. if you want to import all members of all types, use. // for example, import * adds the namespace name with the from keyword after the member. // 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, as in example 6.17.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.