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

Source: Internet
Author: User
Tags constant constant definition functions variables php programming static class
object|php5| Namespace/*
+-------------------------------------------------------------------------------+
| = This article is for Haohappy read <<core PHP programming>>
| = Notes from the chapter classes and objects
| = translation-oriented + personal experience
| = Please do not reprint to avoid any unnecessary trouble that may occur, thank you
| = Welcome to criticize, hope and all PHP enthusiasts to progress together!
+-------------------------------------------------------------------------------+
*/

Section 14th-Namespaces

Named variables, functions, and classes are 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 somewhere else. In a short script, the second problem is the basic question. When you consider reusing your code, the project code after this must avoid using the name you used. In general, reusable code is always included in a function or class, and you need to handle many naming conflicts that may occur. However, a naming conflict can also occur between functions and classes. You can try to avoid this, by adding a prefix to all the classes, or you can use the namespace statement.

namespace keyword to name a piece of code. Outside the code block, the script must use the operator:: plus the name of the namespace to refer to the code block. The same method is used to refer to static class members. In a namespace, code does not need to declare namespaces, which is itself the default. This method is better than the method of adding a prefix. Your code can thus become more compact and readable.

You may want to know if you can establish a layered (nested) namespace. The answer is no. But you can add a colon after the namespace name, and you can call the variables, functions, and classes that do not contain colons in the name. The namespace allows a colon to exist, as long as it is not the first and last character or followed by another colon. A colon in the name of a namespace has no meaning for PHP, but if you use them to differentiate between logical chunks, they can be a good illustration of the parent-child (parent-child) relationship in your code.


* Note: You can use this:
Namespace Animal:dog {}
Namespace Animal:pig {}

Use a colon to illustrate the parent-child relationship.
*/


You may not have anything outside of a namespace statement that contains a function, a class, or a 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 to use namespaces.

Listing 6.17 Using a namespace
<?php
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 tests a class in a namespace
$e = new Core_php:utility::textengine;
Print ($e->uppercase ("from Object"). "<br>");

Test function in namespace tests the functions in a namespace
Print (Core_php:utility::uppercase ("from function"). "<br>");

Bring class into global namespace import classes into global namespaces
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 member of a single namespace, you can specify the type as Constant,function or class, and then write 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 the members of all types, use *.
such as import *

After the member, use the FROM keyword plus the name of the namespace.
such as import class Textengine from core_php:utility;

In short, you want 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.