Section 14th--namespace _php tutorial

Source: Internet
Author: User
Tags constant definition php programming
/*
+-------------------------------------------------------------------------------+
| = This is haohappy read < >
| = Notes in Classes and objects Chapter
| = Translation-based + personal experience
| = Please do not reprint in order to avoid the unnecessary trouble that may occur.
| = Welcome criticism, hope and all the PHP enthusiasts to progress together!
| = PHP5 Research Center: http://blog.csdn.net/haohappy2004
+-------------------------------------------------------------------------------+
*/
The namespace is canceled in the PHP5 version, and the contents of this section are invalid--haohappy note
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
Copy CodeThe code is as follows: Namespace Core_php:utility
{
Class Textengine
{
Public function uppercase ($TEXT)//Uppercase
{
Return (Strtoupper ($text));
}
}
Make Non-oo interface to build a non-OO interface
function uppercase ($text)
{
$e = new Textengine;
Return ($e->uppercase ($text));
}
}
Test class in namespace testing the classes in the namespace
$e = new Core_php:utility::textengine;
Print ($e->uppercase ("from Object"). "
");
Test function in namespace testing the functions in the namespace
Print (Core_php:utility::uppercase ("from function"). "
");
Bring class into global namespace to import classes into a globally named space
Import class textengine from core_php:utility;
$e 2 = new Textengine;
The?> import statement imports a part of the namespace into the global namespace.
To import a single member of a namespace, you can specify a type of Constant,function or class, followed by the name of the member;
such as import class XXX
If you want to import all members of a particular type, you can use * instead of the name;
Import constant * Imports all constants
If you want to import all members of all types, use *.
such as import *
After the member, add the name of the namespace with the FROM keyword.
such as import class Textengine from core_php:utility;
In short you have to write a statement like import * from MyNamespace or import class Textengine from Core_php:utility, as in example 6.17.

http://www.bkjia.com/PHPjc/316928.html www.bkjia.com true http://www.bkjia.com/PHPjc/316928.html techarticle / * +-------------------------------------------------------------------------------+ |= This is haohappy read corephpprogramming |= in ClassesAndObjects a chapter of the note |= translation of the main + personal ...

  • 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.