002.txt 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. Title: Zend 2.0 Namespaces
  2. Version: $Id$
  3. Status: declined
  4. Maintainer: Stig S. Bakken <ssb@php.net>
  5. Created: 2001-09-08
  6. Modified: 2001-09-08
  7. 1. Background/Need
  8. ==================
  9. PHP and Zend 1.0 have come to a point where a lot of reusable code is
  10. being written; from simple functions and classes to entire application
  11. frameworks. It is becoming increasingly difficult to avoid symbol
  12. name collisions with the current scoping methods.
  13. The symbol scopes available in Zend 1.0 are the global scope, the
  14. class scope and the function scope. All scopes but classes may
  15. contain variables, only the class and global scopes may contain
  16. functions, while only the global scope may contain constants and
  17. classes. This means that all of Zend 1.0's scoping methods are
  18. inherently limited for solving symbol name collision problems.
  19. 2. Overview
  20. ===========
  21. Namespaces in Zend 2.0 provide a way to manage the symbol collision
  22. problem by making it possible to define multiple symbol tables able to
  23. contain all types of symbols. Zend will get the notion of a current
  24. namespace, defaulting to the current global one. The current name
  25. space may be changed on a file-by-file basis. Symbols in other name
  26. spaces than the current one may be referenced using a new namespace
  27. operator. It will be possible to "import" symbols from one namespace
  28. into another.
  29. 3. Functionality
  30. ================
  31. 3.1. Namespace Syntax
  32. =====================
  33. The namespace operator ":" is used to refer to symbols in other
  34. namespaces than the current one:
  35. Class: Namespace:class
  36. Function: Namespace:function
  37. Static method: Namespace:class::method
  38. Variable: $Namespace:variable
  39. Constant: Namespace:CONSTANT
  40. Class variable: $Namespace:class::variable
  41. To refer to symbols in the global namespace, symbols are prefixed with
  42. only the namespace operator:
  43. Class: :class
  44. Function: :function
  45. Static method: :class::method
  46. Variable: $:variable
  47. Constant: :CONSTANT
  48. Class variable: $:class::variable
  49. Note: $:variable will effectively be just another syntax for
  50. $GLOBALS['variable'].
  51. A namespace may have a name containing a ":", it is always the last
  52. ":" character in the symbol qualifier that is the actual namespace
  53. operator:
  54. Class: Name:Space:class
  55. Function: Name:Space:function
  56. Static method: Name:Space:class::method
  57. Variable: $Name:Space:variable
  58. Constant: Name:Space:CONSTANT
  59. Class variable: $Name:Space:class::variable
  60. (Here, the ":" between "Name" and "Space" is part of the name, it is
  61. the one after "Space" that is the namespace operator.)
  62. 3.2. Defining Namespaces
  63. ========================
  64. Individual files may define a namespace that will apply to the entire
  65. file. If no "namespace" operator occurs in the file, it will be in
  66. the global namespace:
  67. 1 namespace HTML;
  68. 2
  69. 3 class Form {
  70. 4 function Form() {
  71. 5 // constructor
  72. 6 }
  73. 7 // ...
  74. 8 }
  75. Or with the "nested" name syntax:
  76. 1 namespace HTML:Form;
  77. 2
  78. 3 class Image {
  79. 4 var $src;
  80. 5 function Image($src) {
  81. 6 $this->src = $src;
  82. 7 }
  83. 8 // ...
  84. 9 }
  85. Code executed within the "HTML" namespace may refer to the Form class
  86. as just "Form". Code executed from within other namespaces has to
  87. refer to it as "HTML:Form". The "namespace" statement must occur
  88. before any other statements in the file.
  89. # [ssb 2001-09-08]:
  90. # Should it be possible to "add" symbols to a namespace by including a
  91. # second file with the same namespace statement?
  92. 3.3. Importing Symbols
  93. ======================
  94. It is possible to import symbols from another namespace into the
  95. current one with the "import" statement:
  96. import * from HTML; // all symbols
  97. import Form from HTML; // single symbols
  98. import Form,Table from HTML; // multiple symbols
  99. There is a potential for name clashes between symols of different
  100. types that have the same qualifier syntax. These are resolved in this
  101. order: class, function, constant.
  102. Optionally, the symbol type may be explicitly given to import (as
  103. "class", "function", "variable" or "constant"):
  104. import class Form from HTML;
  105. And finally, you may import all symbols of a given type:
  106. import constant * from HTML:Table;
  107. The namespace with its symbols must already be defined before using
  108. "import".
  109. 4. Compatibility Notes
  110. ======================
  111. Old code that does not take advantage of namespaces will run without
  112. modifications.
  113. 5. Dependencies
  114. ===============
  115. The class variable syntax depends on this class variables being
  116. implemented in the new ZE2 object model.
  117. 6. Acknowledgements
  118. ===================
  119. Andi Gutmans <andi@zend.com> and Zeev Suraski <zeev@zend.com> for
  120. initial ZE2 namespaces proposal
  121. Dean Hall <php@apt7.com> for the initial symbol qualification syntax