autoloading-design.xml 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="learning.autoloading.design">
  4. <title>Goals and Design</title>
  5. <sect2 id="learning.autoloading.design.naming">
  6. <title>Class Naming Conventions</title>
  7. <para>
  8. To understand autoloading in Zend Framework, first you need to understand the relationship
  9. between class names and class files.
  10. </para>
  11. <para>
  12. Zend Framework has borrowed an idea from <ulink url="http://pear.php.net/">PEAR</ulink>,
  13. whereby class names have a 1:1 relationship with the filesystem. Simply put, the underscore
  14. character ("_") is replaced by a directory separator in order to resolve the path to the
  15. file, and then the suffix ".php" is added. For example, the class "Foo_Bar_Baz" would
  16. correspond to "Foo/Bar/Baz.php" on the filesystem. The assumption is also that the classes
  17. may be resolved via PHP's <constant>include_path</constant> setting, which allows both
  18. <methodname>include</methodname> and <methodname>require</methodname> to find the filename
  19. via a relative path lookup on the <constant>include_path</constant>.
  20. </para>
  21. <para>
  22. Additionally, per PEAR as well as the <ulink
  23. url="http://php.net/userlandnaming.tips">PHP project</ulink>, we use and recommend
  24. using a vendor or project prefix for your code. What this means is that all classes you
  25. write will share a common class prefix; for example, all code in Zend Framework has the
  26. prefix "Zend_". This naming convention helps prevent naming collisions. Within Zend
  27. Framework, we often refer to this as the "namespace" prefix; be careful not to confuse
  28. it with PHP's native namespace implementation.
  29. </para>
  30. <para>
  31. Zend Framework follows these simple rules internally, and our coding standards encourage
  32. that you do so as well for all library code.
  33. </para>
  34. </sect2>
  35. <sect2 id="learning.autoloading.design.autoloader">
  36. <title>Autoloader Conventions and Design</title>
  37. <para>
  38. Zend Framework's autoloading support, provided primarily via
  39. <classname>Zend_Loader_Autoloader</classname>, has the following goals and design elements:
  40. </para>
  41. <itemizedlist>
  42. <listitem>
  43. <para>
  44. <emphasis role="strong">Provide namespace matching.</emphasis> If the class
  45. namespace prefix is not in a list of registered namespaces, return false
  46. immediately. This allows for more optimistic matching, as well as fallback to other
  47. autoloaders.
  48. </para>
  49. </listitem>
  50. <listitem>
  51. <para>
  52. <emphasis role="strong">Allow the autoloader to act as a fallback
  53. autoloader.</emphasis> In the case where a team may be widely distributed, or
  54. using an undetermined set of namespace prefixes, the autoloader should still be
  55. configurable such that it will attempt to match any namespace prefix. It will be
  56. noted, however, that this practice is not recommended, as it can lead to unnecessary
  57. lookups.
  58. </para>
  59. </listitem>
  60. <listitem>
  61. <para>
  62. <emphasis role="strong">Allow toggling error suppression.</emphasis> We feel -- and
  63. the greater PHP community does as well -- that error suppression is a bad idea. It's
  64. expensive, and it masks very real application problems. So, by default, it should be
  65. off. However, if a developer <emphasis>insists</emphasis> that it be on, we allow
  66. toggling it on.
  67. </para>
  68. </listitem>
  69. <listitem>
  70. <para>
  71. <emphasis role="strong">Allow specifying custom callbacks for
  72. autoloading.</emphasis> Some developers don't want to use
  73. <methodname>Zend_Loader::loadClass()</methodname> for autoloading, but still want to
  74. make use of ZF's mechanisms. <classname>Zend_Loader_Autoloader</classname> allows
  75. specyfing an alternate callback for autoloading.
  76. </para>
  77. </listitem>
  78. <listitem>
  79. <para>
  80. <emphasis role="strong">Allow manipulation of the SPL autoload callback
  81. chain.</emphasis> The purpose of this is to allow specifying additional
  82. autoloaders -- for instance, resource loaders for classes that don't have a 1:1
  83. mapping to the filesystem -- to be registered before or after the primary ZF
  84. autoloader.
  85. </para>
  86. </listitem>
  87. </itemizedlist>
  88. </sect2>
  89. </sect1>