Home  >  Article  >  Backend Development  >  PHP Kernel-Namespace Tips Sharing

PHP Kernel-Namespace Tips Sharing

黄舟
黄舟Original
2017-03-09 09:38:591290browse

In Wikipedia, the definition of namespace is: Namespace (English: Namespace) represents an identifier ( identifier) ​​context .

An identifier can be defined in multiple namespaces, and its meanings in different namespaces are independent of each other. In programming languages, a namespace is a special scope that contains identifiers within that scope and is itself represented by an identifier, so that a series of logically related identifiers are represented by an identifier. Identifiers are organized. Function and class scopes can be thought of as implicit namespaces, which are inextricably linked to visibility, accessibility, and object lifetime.


Namespace can be seen as a way to encapsulate things, and you can also see Writing is a form of organizing code structure. This abstract concept and organizational form can be seen in many languages. In PHP, namespaces are used to solve two types of problems encountered when creating reusable code such as classes or functions when writing class libraries or applications:

1. There is a name conflict between user-written code and PHP internal classes/functions/constants or third-party classes/functions/constants.

2. Create an alias (or short) name for a very long identifier name (usually defined to alleviate the first type of problem), Improve the readability of source code.


##PHP supports the namespace feature starting from version 5.3.0. See an example of defining and using a namespace:


##

<?php
namespace tipi;
class Exception {
    public static $var = &#39;think in php internal&#39;;
}
 
const E_ALL = "E_ALL IN Tipi";
 
function strlen(){
    echo &#39;strlen in tipi&#39;;
}
 
echo Exception::$var;
echo strlen(Exception::$var);

1. Example:



#2. Definition of namespace


3. Naming Usage of space

Take function calling as an example. When a function needs to be called, the zend_do_begin_function_call function will be called. In this function, the function name is checked when the namespace is used, and the function called is zend_resolve_non_class_name. In the zend_resolve_non_class_name function, a judgment is made based on the type and the relevant results are returned:

1.Fully qualified name function: The program will first make this judgment, and the basis for its judgment is whether the first character is "\". In this case, it will be returned directly during parsing. For example, a global call starting with \ like \strlen or a call like \tipi\Exception defined previously.

2.All unqualified names and qualified names (non-fully qualified names): Determine whether it is an alias based on the current import rule program, and Take the corresponding namespace name from the HashTable where aliases are stored during compilation, and merge it with the existing function name. The storage and generation of aliases will be explained in the following content,

3.Inside the namespace: All are not imported according to The qualified names of rule transformations are preceded by the current namespace name. Finally, it is judged whether it is in the current namespace. In the end, the program will return a function name that merges the namespace.

4. Aliases/Imports

Allow references or imports by aliases The external fully qualified name is an important feature of the namespace.

#This is somewhat similar to how you can create symbolic links to other files or directories in a Unix-like file system. PHP namespace support There are two ways to use aliases or imports: using aliases for class names, or using aliases for namespace names.


The above is the detailed content of PHP Kernel-Namespace Tips Sharing. For more information, please follow other related articles on the PHP Chinese website!

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