Section 14th -- namespace -- ClassesandObjectsinPHP5 [14] _ PHP Tutorial

Source: Internet
Author: User
Tags constant definition
Section 14th -- namespace -- ClassesandObjectsinPHP5 [14]. Section 14th -- namespace naming variables, functions and classes are quite difficult, except for the ease of understanding of variable names, worry about whether the name has been used in section 14th somewhere else-namespace
It is quite difficult to name variables, functions, and classes. in addition to taking into account the ease of understanding of the variable name, you also need 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, 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 this method:
Namespace animal: dog {}
Namespace animal: pig {}
Use a colon to describe the parent-child relationship.
*/
You may not include any function, class, or constant definition 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
Namespace core_php: utility
{
Class textEngine
{
Public function uppercase ($ text) // uppercase
{
Return (strtoupper ($ text ));
}
}

In section 14th of lifecycle, naming variables in the namespace is quite difficult for functions and classes. in addition to taking into account the ease of understanding of variable names, you must also worry about whether the name has been used elsewhere...

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.