Home > Backend Development > PHP Tutorial > Section 14--Namespace--ClassesandObjectsinPHP514_PHP Tutorial

Section 14--Namespace--ClassesandObjectsinPHP514_PHP Tutorial

WBOY
Release: 2016-07-13 17:24:13
Original
837 people have browsed it

/* +-------------------------------------------------- --------------------------------+ | = This article is read by Haohappy> | = Classes and Objects 1 Chapter's notes | = Translation + personal experience | = To avoid unnecessary trouble, please do not reprint, thank you | = Criticisms and corrections are welcome, and I hope to make progress together with all PHP enthusiasts! +------- -------------------------------------------------- -----------------------+ */ Section 14 - Namespace naming variables, functions and classes is quite difficult, except that you have to take into account the variables The name needs to be easy to understand, and you have to worry about whether the name has been used somewhere else. In a small script, the second problem is the basic problem. When you consider reusing your code, the project code after this must be avoided Use the names you use. Generally speaking, reusable code is always contained in a function or class, and there are many naming conflicts that can occur. But naming conflicts can also occur between functions and classes. You can try to avoid them In this case, either by prefixing all classes, or you can use the namespace statement. The Namespace keyword names a block of code. Outside this block of code, the script must be referenced using the operator:: followed by the name of the namespace. This code block. The same way is used to reference static class members. Code within the namespace does not need to declare the namespace, it defaults to it. This method is better than adding a prefix. Your code can become more flexible Compact and readable. You may wonder if it is possible to create hierarchical (nested) namespaces. The answer is no. But you can add a colon after the namespace name, and you can call it again without the colon in the name. variables, functions and classes. Colons are allowed in namespaces as long as they are not the first character and the last character or followed by another colon. Colons in namespace names have no meaning to PHP, but if you use them Separate logical blocks, they can be a good way to illustrate the parent-child relationship in your code. /* Note: You can use this: namespace animal:dog {} namespace animal:pig {} Use colons Specify parent-child relationships. */ You may not include anything other than function, class, or constant definitions within a namespace statement. This will prevent you from using them to improve older function libraries that use global variables. Namespaces are best suited For object-oriented. Constants within 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 classes in namespace $e = new core_php:utility::textEngine; print($e->uppercase("from object") . "


"); //test function in namespace Test naming Function print(core_php:utility::uppercase("from function") . "
"); //bring class into global namespace Import class textEngine from core_php:utility; $e2 = new textEngine; ?> The Import statement imports a certain 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; // For example, import class For example, after import *, use the from keyword and the name of the namespace after the member. //For example, 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, Just like in Example 6.17.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532150.htmlTechArticle/* +--------------------- -------------------------------------------------- --------+ | = This article is read by Haohappy> | = Notes from the Chapter Classes and Objects| = Translation + personal experience...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template