Home  >  Article  >  Backend Development  >  PHP namespace_PHP tutorial

PHP namespace_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:24:58843browse

Naming variables, functions and classes is quite difficult. In addition to considering that the name of the variable should be easy to understand, you also have to worry about whether the name has been used somewhere else. In a small script, the first The two questions are fundamental. When you consider reusing your code, the code in subsequent projects must avoid using the names you have used. Generally speaking, reusable code is always contained in functions or classes, which requires a lot of processing Possible naming conflicts. But naming conflicts can also occur between functions and classes. You can try to avoid this by prefixing all classes, or you can use the namespace statement.

The Namespace keyword names a block of code. Outside this block of code, scripts must reference this block of code using operator:: followed by the name of the namespace. The same method is used to reference static class members. Code within the namespace does not You need to declare the namespace, which is the default. This method is better than adding a prefix. Your code will become more compact and readable.

You may wonder whether 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 variables, functions and classes again that do not contain a colon in the name. Colons are allowed in namespaces as long as they are not The first and last characters or followed by another colon. Colons in namespace names don't mean anything to PHP, but if you use them to separate logical chunks, they can tell a good story about your The parent-child relationship in the code.

Note: You can use this:

namespace animal:dog {}namespace animal:pig {}

Use colons to illustrate the parent-child relationship.

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. Constants within a namespace use the same syntax as constants within a class.

Example 1 shows how to use a namespace.

Example 1

<?php    namespace core_php:utility    {        class textEngine        {            public function uppercase($text) //大写            {                return(strtoupper($text));            }        }        //make non-OO interface 建立一个非OO的接口        function uppercase($text)        {            $e = new textEngine;            return($e->uppercase($text));        }    }    //test class in namespace 测试命名空间中的类    $e = new core_php:utility::textEngine;    print($e->uppercase("from object") . "<br>");    //test function in namespace 测试命名空间中的函数    print(core_php:utility::uppercase("from function") . "<br>");    //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 members of a single namespace, you can specify the type as constant, function or class, then write the name of the member;

//For example, import class XXX

If you want to import all members of a specific type, you can use * instead of the name ;

//For example, import constant * to import all constants

If you want to import all members of all types, just use *.

//For example, import *

After the member, use the from keyword plus the name of the namespace.

//For example, import class textEngine from core_php:utility;

In short, you have to write something like import * from myNamespace or import class textEngine from core_php:utility, as shown in Figure 1.

http://www.bkjia.com/PHPjc/446716.html
www.bkjia.com

truehttp: //www.bkjia.com/PHPjc/446716.htmlTechArticleNaming variables, functions and classes is quite difficult. In addition to taking into account that the names of variables should be easy to understand, Worry about whether this name has already been used somewhere else. In a small script, section...
Statement:
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