____php Kernel Exploration: namespaces

Source: Internet
Author: User

Tags: style http color io os using ar strong file

PHP Kernel Exploration: namespaces

Namespaces are a special type of scope



Thank you for reference or the original www.php-internal.com server June spent a total of 81.974 MS 3 database queries, and strive to provide you with this page. Try reading mode? I'd like to hear your suggestions.

In Wikipedia, the definition of a namespace is: the namespace (English: Namespace) represents the context of the identifier (identifier). An identifier can be defined in more than one namespace, and its meaning in different namespaces is mutually irrelevant. In a programming language, a namespace is a special scope that contains an identifier that is in that scope, and itself is represented by an identifier, which organizes a series of logically related identifiers together with an identifier. The scopes of functions and classes can be seen as implicit namespaces, which are inextricably linked to visibility, accessibility, and object life cycles.

Namespaces can be thought of as a way to encapsulate things, but also as a form of organization code structure that can be seen in many languages. In PHP, namespaces are used to solve two types of problems encountered when writing a class library or application to create reusable code such as classes or functions:

    1. User-written code conflicts with the name of a class/function/constant or third-party class/function/constant inside PHP.
    2. Creates an alias (or short) name for a very long identifier name (usually defined to alleviate the first type of problem), improving the readability of the source code.

PHP supports namespace features starting with version 5.3.0. Look at an example that defines and uses a namespace:

01 <?php
02 namespace tipi;
03 classException {
04     publicstatic $var= ‘think in php internal‘;
05 }
06  
07 constE_ALL = "E_ALL IN Tipi";
08  
09 functionstrlen(){
10     echo‘strlen in tipi‘;
11 }
12  
13 echoException::$var;
14 echostrlen(Exception::$var);
15 ?>

As shown above, a namespace tipi is defined, within which a exception class is defined, a E_all constant and a function strlen. These classes, constants, and functions PHP are implemented by default. Without this namespace, declaring these classes, constants, or functions would be an error that the function repeats or the class repeats, and the definition of the constant will not succeed.

In the PHP language, namespaces are defined by the namespace keyword, which can include any valid PHP code within a namespace, but its scope is limited to classes, constants, and functions. Syntactically, PHP supports defining multiple namespaces in a single file, but it is not recommended for this type of code organization. When you need to combine the code in the global non-namespace with the code in the namespace, the global code must be enclosed in curly braces with a namespace statement without a name.

At this point, consider how the namespace definition is implemented in the PHP kernel. When there are multiple identical functions or classes in multiple namespaces, how do you differentiate between them? How does a function within a namespace be called?

Definition of namespaces

The implementation of namespaces in PHP is simple, whether it is a function, a class, or a constant, in the process of declaring the namespace with the defined function name to \ Merge together, as the function name or class name stored in its corresponding container. As the Exception class in the example above, the last stored class name is tipi\exception. The cost of this implementation and the adjustment to the entire code structure are minimal for the entire PHP implementation architecture.

Let's take the exception class as an example to illustrate the implementation of the entire namespace. The keyword for the namespace implementation is namespace, and from this keyword we can find the function that handles this keyword at compile time as zend_do_begin_namespace. In this function, the key is in the assignment of CG (Current_namespace), which is useful when declaring a later class declaration or function.

As we said earlier, the implementation of the class declaration invokes the Zend_do_begin_class_declaration function in the zend/zend_complie.c file at compile time, and the processing code for the namespace in this function is as follows:

01 if(CG(current_namespace)) {
02     /* Prefix class name with name of current namespace */
03     znode tmp;
04  
05     tmp.u.constant = *CG(current_namespace);
06     zval_copy_ctor(&tmp.u.constant);
07     zend_do_build_namespace_name(&tmp, &tmp, class_name TSRMLS_CC);
08     class_name = &tmp;
09     efree(lcname);
10     lcname = zend_str_tolower_dup(Z_STRVAL(class_name->u.constant), Z_STRLEN(class_name->u.constant));
11 }

The purpose of this code is to prefix the class name with the namespace if the namespace is currently present, as mentioned in the example Tipi\exception class above, where the addition of tipi\ is performed. In the Zend_do_build_namespace_name function, the Zend_do_build_full_name function is eventually called to implement the merge of the class name. The same name merge operation exists in the declaration of functions and constants. This is also why namespaces are valid only for classes, constants, and functions.

Using namespaces

As an example of a function call, the Zend_do_begin_function_call function is called when a function needs to be called. In this function, when a namespace is used, the function name is checked, and the function called is zend_resolve_non_class_name. In the Zend_resolve_non_class_name function, it is judged by type and returns the relevant result:

    1. Fully qualified name function: The program first makes this judgment, and its judgment is based on whether the first character is "\", in which case it is returned directly at parse time. A global call, such as \strlen, that begins with \ or a \tipi\exception call similar to the previously defined.
    2. All unqualified and qualified names (non-fully qualified names): Determines whether an alias is based on the current import rule program, and takes the corresponding namespace name from the Hashtable that stores the alias during compilation, merging it with the existing function name. The storage and generation of aliases is explained in the following sections,
    3. Inside the namespace: all qualified names that are not translated according to the import rule are preceded by the current namespace name. Finally, if the current namespace is determined, the final program returns a function name that incorporates the namespace.
Alias/import

Allowing the external fully qualified name to be referenced or imported through an alias is an important feature of the namespace. This is somewhat similar to the ability to create symbolic connections to other files or directories in a UNIX-like file system. The PHP namespace supports two ways to use aliases or imports: Use aliases for class names, or use aliases for namespace names.

PHP does not support importing functions or constants.

In PHP, aliases are implemented using the operator use. So we can find from the source code that the function called at compile time is zend_do_use. Aliases are stored in CG (Current_import) in the process of compiling to intermediate code, which is a hashtable. Zend_do_use the implementation of the entire function is basically a process of finding, judging whether it is wrong, and finally writing to the Hashtable. There is an import process for namespaces and class names, but not for constants and functions, which is the root cause of PHP's unsupported import of functions or constants.

Extended Reading

The list of topics for this article is as follows:

  1. PHP Kernel Explorer: Starting with the SAPI interface
  2. PHP kernel exploration: Start and end of a single request
  3. PHP kernel exploration: One-time request life cycle
  4. PHP Kernel Exploration: single-process SAPI life cycle
  5. PHP kernel Exploration: SAPI lifecycle of multiple processes/threads
  6. PHP Kernel Explorer: Zend Engine
  7. PHP Kernel Explorer: Explore SAPI again
  8. PHP kernel Discovery: Apache module Introduction
  9. PHP Kernel Explorer: PHP support via MOD_PHP5
  10. PHP kernel exploration: Apache Run with hook function
  11. PHP Kernel Explorer: embedded PHP
  12. PHP Kernel Explorer: PHP fastcgi
  13. PHP kernel exploration: How to execute PHP scripts
  14. PHP Kernel Explorer: PHP script execution details
  15. PHP kernel exploration: opcode opcode
  16. PHP kernel Explorer: PHP opcode
  17. PHP kernel exploration: Interpreter execution process
  18. PHP Kernel Exploration: Variables overview
  19. PHP kernel exploration: variable storage and type
  20. PHP Kernel Explorer: Hash table in PHP
  21. PHP Kernel Exploration: Understanding the hash table in Zend
  22. PHP kernel exploration: PHP hash Algorithm design
  23. PHP kernel Exploration: translating an article hashtables
  24. PHP Kernel exploration: What is a hash collision attack?
  25. PHP Kernel exploration: implementation of constants
  26. PHP kernel exploration: Storage of variables
  27. PHP kernel exploration: Types of variables
  28. PHP Kernel Explorer: Variable value operation
  29. PHP Kernel exploration: Creation of variables
  30. PHP kernel exploration: pre-defined variables
  31. PHP Kernel Explorer: variable retrieval
  32. PHP kernel Exploration: Variable type conversion
  33. PHP Kernel exploration: implementation of weakly typed variables
  34. PHP Kernel exploration: implementation of static variables
  35. PHP Kernel Explorer: Variable type hints
  36. PHP kernel exploration: The life cycle of a variable
  37. PHP Kernel Exploration: variable assignment and destruction
  38. PHP Kernel exploration: variable scope
  39. PHP kernel Explorer: Weird variable names
  40. PHP Kernel Explorer: variable value and type storage
  41. PHP Kernel Explorer: global variables
  42. PHP kernel Exploration: Conversion of variable types
  43. PHP kernel Exploration: The memory management begins
  44. PHP Kernel Explorer: Zend Memory manager
  45. PHP Kernel Explorer: PHP's memory management
  46. PHP Kernel Exploration: Application and destruction of memory
  47. PHP Kernel Exploration: reference count vs. write-time replication
  48. PHP kernel exploration: PHP5.3 garbage collection mechanism
  49. PHP Kernel Explorer: Cache in memory management
  50. PHP Kernel Exploration: write-time copy cow mechanism
  51. PHP kernel Exploration: arrays and Linked lists
  52. PHP kernel exploration: Using the Hash Table API
  53. PHP kernel exploration: array manipulation
  54. PHP kernel Exploration: Array source code Analysis
  55. PHP Kernel Exploration: Classification of functions
  56. PHP kernel Exploration: internal structure of functions
  57. PHP Kernel exploration: function structure transformation
  58. PHP Kernel Exploration: The process of defining a function
  59. PHP kernel Exploration: Parameters for functions
  60. PHP kernel exploration: zend_parse_parameters function
  61. PHP Kernel exploration: function return value
  62. PHP kernel exploration: formal parameter return value
  63. PHP Kernel exploration: function invocation and execution
  64. PHP kernel exploration: Referencing and function execution
  65. PHP kernel exploration: anonymous functions and closures
  66. PHP Kernel Exploration: object-oriented opening
  67. PHP kernel Exploration: The structure and implementation of classes
  68. PHP kernel exploration: member Variables for classes
  69. PHP Kernel Exploration: Member Methods for classes
  70. PHP Kernel Exploration: class prototype Zend_class_entry
  71. PHP kernel exploration: Definition of class
  72. PHP Kernel Explorer: Access control
  73. PHP kernel exploration: inheritance, polymorphism and abstract classes
  74. PHP Kernel Exploration: magic function and delay binding
  75. PHP kernel Exploration: Preserving classes and special classes
  76. PHP Kernel Explorer: objects
  77. PHP kernel Exploration: Creating object instances
  78. PHP Kernel Explorer: Object properties Read and write
  79. PHP Kernel Exploration: namespaces
  80. PHP kernel exploration: Defining interfaces
  81. PHP kernel Exploration: Inheritance and Implementation interface
  82. PHP Kernel Exploration: resource resource type
  83. PHP Kernel Explorer: Zend virtual machine
  84. PHP Kernel Exploration: Lexical parsing of virtual machines
  85. PHP Kernel Explorer: virtual machine Syntax analysis
  86. PHP kernel exploration: Execution of intermediate code opcode
  87. PHP Kernel Exploration: Code encryption and decryption
  88. PHP kernel exploration: zend_execute specific execution process
  89. PHP kernel exploration: Reference and counting rules for variables
  90. PHP kernel exploration: New garbage collection Mechanism description

____php Kernel Exploration: namespaces

Related Article

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.

Tags Index: