autoloading-design.xml 5.2 KB

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