Section 14th-namespaces-Classes and Objects in PHP5 [14]_php tutorial

Source: Internet
Author: User
Section 14th-Namespaces
Naming variables, functions, and classes is difficult, except to take into account the name of the variable to be easy to understand, but also 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, after that the project code must avoid using the naming you have used. In general, reusable code is always included in a function or class, and there are many possible naming conflicts that need to be handled. However, a naming conflict can also occur between functions and classes. You can try to avoid this by adding prefixes before all classes, or you can use the namespace statement.
namespace keyword to name a piece of code. Outside this block of code, the script must refer to the code block with the operator:: plus the name of the namespace. Referencing static class Members is also used in the same way. In a namespace, code does not need to declare a namespace, which itself is the default. This method is better than the method of adding prefixes. Your code can become more compact and readable.
You might want to know if you can create a layered (nested) namespace. The answer is no. But you can add a colon to the namespace name, and you can call variables, functions, and classes that do not contain colons in the name. The namespace allows the presence of a colon, as long as it is not the first and last character or then another colon. The colon in the name of the namespace has no meaning for PHP, but if you use them to distinguish logical chunks, they can well describe the parent-child (parent-child) relationship in your code.
/* Note: You can use this:
Namespace Animal:dog {}
Namespace Animal:pig {}
Use a colon to describe the parent-child relationship.
*/
You might not have anything outside of a namespace statement that contains a function, class, or constant definition. This will prevent you from using them to improve the old library of functions that use global variables. Namespaces are best suited for object-oriented. Constants in a namespace use the same syntax as constants in a class.
Example 6.17 shows how namespaces are used.
Listing 6.17 Using a namespace
Namespace Core_php:utility
{
Class Textengine
{
Public function uppercase ($TEXT)//Uppercase
{
Return (Strtoupper ($text));
}
}

http://www.bkjia.com/PHPjc/445272.html www.bkjia.com true http://www.bkjia.com/PHPjc/445272.html techarticle the 14th section-namespace naming variables, functions and classes is difficult, except to take into account the name of the variable to be easy to understand, but also to 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.