README.namespaces 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. Design
  2. ======
  3. Main assumption of the model is that the problem that we are to solve is the
  4. problem of the very long class names in PHP libraries. We would not attempt
  5. to take autoloader's job or create packaging model - only make names
  6. manageable.
  7. Namespaces are defined the following way:
  8. Zend/DB/Connection.php:
  9. <?php
  10. namespace Zend\DB;
  11. class Connection {
  12. }
  13. function connect() {
  14. }
  15. ?>
  16. Namespace definition does the following:
  17. All class and function names inside are automatically prefixed with
  18. namespace name. Inside namespace, local name always takes precedence over
  19. global name. Several files may be using the same namespace.
  20. The namespace declaration statement must be the very first statement in
  21. the file. The only exception is "declare" statement that can be used before.
  22. Every class and function in a namespace can be referred to by the full name
  23. - e.g. Zend\DB\Connection or Zend\DB\connect - at any time.
  24. <?php
  25. require 'Zend/Db/Connection.php';
  26. $x = new Zend\DB\Connection;
  27. Zend\DB\connect();
  28. ?>
  29. Namespace or class name can be imported:
  30. <?php
  31. require 'Zend/Db/Connection.php';
  32. use Zend\DB;
  33. use Zend\DB\Connection as DbConnection;
  34. $x = new Zend\DB\Connection();
  35. $y = new DB\connection();
  36. $z = new DbConnection();
  37. DB\connect();
  38. ?>
  39. The use statement only defines name aliasing. It may create name alias for
  40. namespace or class. The simple form of statement "use A\B\C\D;" is
  41. equivalent to "use A\B\C\D as D;". The use statement can be used at any
  42. time in the global scope (not inside function/class) and takes effect from
  43. the point of definition down to the end of file. It is recommended however to
  44. place the use statements at the beginning of the file. The use statements have
  45. effect only on the file where they appear.
  46. The special "empty" namespace (\ prefix) is useful as explicit global
  47. namespace qualification. All class and function names started from \
  48. interpreted as global.
  49. <?php
  50. namespace A\B\C;
  51. $con = \mysql_connect(...);
  52. ?>
  53. A special constant __NAMESPACE__ contains the name of the current namespace.
  54. It can be used to construct fully-qualified names to pass them as callbacks.
  55. <?php
  56. namespace A\B\C;
  57. function foo() {
  58. }
  59. set_error_handler(__NAMESPACE__ . "\foo");
  60. ?>
  61. In global namespace __NAMESPACE__ constant has the value of empty string.
  62. Names inside namespace are resolved according to the following rules:
  63. 1) all qualified names are translated during compilation according to
  64. current import rules. So if we have "use A\B\C" and then "C\D\e()"
  65. it is translated to "A\B\C\D\e()".
  66. 2) unqualified class names translated during compilation according to
  67. current import rules. So if we have "use A\B\C" and then "new C()" it
  68. is translated to "new A\B\C()".
  69. 3) inside namespace, calls to unqualified functions that are defined in
  70. current namespace (and are known at the time the call is parsed) are
  71. interpreted as calls to these namespace functions.
  72. 4) inside namespace, calls to unqualified functions that are not defined
  73. in current namespace are resolved at run-time. The call to function foo()
  74. inside namespace (A\B) first tries to find and call function from current
  75. namespace A\B\foo() and if it doesn't exist PHP tries to call internal
  76. function foo(). Note that using foo() inside namespace you can call only
  77. internal PHP functions, however using \foo() you are able to call any
  78. function from the global namespace.
  79. 5) unqualified class names are resolved at run-time. E.q. "new Exception()"
  80. first tries to use (and autoload) class from current namespace and in case
  81. of failure uses internal PHP class. Note that using "new A" in namespace
  82. you can only create class from this namespace or internal PHP class, however
  83. using "new \A" you are able to create any class from the global namespace.
  84. 6) Calls to qualified functions are resolved at run-time. Call to
  85. A\B\foo() first tries to call function foo() from namespace A\B, then
  86. it tries to find class A\B (__autoload() it if necessary) and call its
  87. static method foo()
  88. 7) qualified class names are interpreted as class from corresponding
  89. namespace. So "new A\B\C()" refers to class C from namespace A\B.
  90. Examples
  91. --------
  92. <?php
  93. namespace A;
  94. foo(); // first tries to call "foo" defined in namespace "A"
  95. // then calls internal function "foo"
  96. \foo(); // calls function "foo" defined in global scope
  97. ?>
  98. <?php
  99. namespace A;
  100. new B(); // first tries to create object of class "B" defined in namespace "A"
  101. // then creates object of internal class "B"
  102. new \B(); // creates object of class "B" defined in global scope
  103. ?>
  104. <?php
  105. namespace A;
  106. new A(); // first tries to create object of class "A" from namespace "A" (A\A)
  107. // then creates object of internal class "A"
  108. ?>
  109. <?php
  110. namespace A;
  111. B\foo(); // first tries to call function "foo" from namespace "A\B"
  112. // then calls method "foo" of internal class "B"
  113. \B\foo(); // first tries to call function "foo" from namespace "B"
  114. // then calls method "foo" of class "B" from global scope
  115. ?>
  116. The worst case if class name conflicts with namespace name
  117. <?php
  118. namespace A;
  119. A\foo(); // first tries to call function "foo" from namespace "A\A"
  120. // then tries to call method "foo" of class "A" from namespace "A"
  121. // then tries to call function "foo" from namespace "A"
  122. // then calls method "foo" of internal class "A"
  123. \A\foo(); // first tries to call function "foo" from namespace "A"
  124. // then calls method "foo" of class "A" from global scope
  125. ?>
  126. TODO
  127. ====
  128. * Support for namespace constants?
  129. * performance problems
  130. - calls to internal functions in namespaces are slower, because PHP first
  131. looks for such function in current namespace
  132. - calls to static methods are slower, because PHP first tries to look
  133. for corresponding function in namespace
  134. * Extend the Reflection API?
  135. * Add ReflectionNamespace class
  136. + getName()
  137. + getClasses()
  138. + getFunctions()
  139. + getFiles()
  140. * Add getNamespace() methods to ReflectionClass and ReflectionFunction
  141. * Rename namespaces to packages?