coding_standard.xml 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  1. <appendix id="coding-standard">
  2. <title>Стандарт написання PHP коду в Zend Framework</title>
  3. <sect1 id="coding-standard.overview">
  4. <title>Огляд</title>
  5. <sect2 id="coding-standard.overview.scope">
  6. <title>Сфера застосування</title>
  7. <para>
  8. Цей документ надає вказівки щодо форматування коду та документації для розробників
  9. та команд які роблять свій вклад в розробку Zend Framework. Багато розробників
  10. що використовують Zend Framework також знаходять корисним слідувати
  11. цим стандартам щоб писати свій код у відповідності до всього коду Zend Framework.
  12. Варто зазначити що докладено великих зусиль щоб як найбільш повно описати стандарти кодування.
  13. <note>
  14. <para>
  15. Примітка: Sometimes developers consider the establishment of a standard more
  16. important than what that standard actually suggests at the most detailed
  17. level of design. Інколи розробники надають впровадженню стандарту важливішого
  18. значення ніж те, що стандарт дійсно пропонує, на найбільш деталізованому
  19. рівні розробки. Вказівки в стандартах кодування Zend Framework захоплюють
  20. практичні підходи що добре себе зарекомендували в проекті розробки Zend Framework.
  21. Дозволяється модифікувати ці стандарти або використовувати їх в незмінному
  22. стані у відповідності до положень нашої
  23. <ulink url="http://framework.zend.com/license">ліцензії</ulink>.
  24. </para>
  25. </note>
  26. Теми що висвітлюються в стандартах кодування Zend Framework включають:
  27. <itemizedlist>
  28. <listitem>
  29. <para>Форматування <acronym>PHP</acronym> файлів</para>
  30. </listitem>
  31. <listitem>
  32. <para>Принципи іменування</para>
  33. </listitem>
  34. <listitem>
  35. <para>Стиль написання коду</para>
  36. </listitem>
  37. <listitem>
  38. <para>Вбудована документація</para>
  39. </listitem>
  40. </itemizedlist>
  41. </para>
  42. </sect2>
  43. <sect2 id="coding-standard.overview.goals">
  44. <title>Цілі</title>
  45. <para>
  46. Дотримання стандартів написання коду є важливим для будь якого проекту,
  47. зокрема коли над одним проектом працюють кілька розробників.
  48. Стандарти написання коду забезпечують високу якість,
  49. меншу кількість помилок, та легкий супровід коду.
  50. </para>
  51. </sect2>
  52. </sect1>
  53. <sect1 id="coding-standard.php-file-formatting">
  54. <title>Форматування PHP файлів</title>
  55. <sect2 id="coding-standard.php-file-formatting.general">
  56. <title>Загальні положення</title>
  57. <para>
  58. Заборонено ставити закриваючий тег ("?>") для файлів що містять виключно
  59. <acronym>PHP</acronym> код. Закриваючий тег необов'язковий для <acronym>PHP</acronym>,
  60. та його відсутність допомагає уникнути випадкового попадання у відповідь сервера
  61. пробілу що опинився в кінці файлу.
  62. </para>
  63. <note>
  64. <para>
  65. <emphasis>Важливо</emphasis>: Включення додаткового бінарного коду як це дозволено при
  66. використанні <code>__HALT_COMPILER()</code> заборонено для всіх <acronym>PHP</acronym>
  67. файлів в проекті Zend framework та похідних файлах. Використання такої можливості
  68. дозволено лише для деяких сценаріїв встановлення.
  69. </para>
  70. </note>
  71. </sect2>
  72. <sect2 id="coding-standard.php-file-formatting.indentation">
  73. <title>Відступи</title>
  74. <para>Використовувати відступ в 4 пробіли, без табуляції.</para>
  75. </sect2>
  76. <sect2 id="coding-standard.php-file-formatting.max-line-length">
  77. <title>Максимальна довжина рядка</title>
  78. <para>
  79. Дотримуватися довжини рядка в 80 символів, тобто розробники повинні намагатись тримати
  80. код настільки близько до 80-ої колонки наскільки це практично можливо.
  81. Проте, довші рядки дозволені. Максимальна довжина будь-якого рядка <acronym>PHP</acronym> коду
  82. не повинна перевищувати 120 символів.
  83. </para>
  84. </sect2>
  85. <sect2 id="coding-standard.php-file-formatting.line-termination">
  86. <title>Завершення рядків</title>
  87. <para>
  88. Завершення рядка в стандартний спосіб для текстових файлів Unix. Рядки повинні завершуватися
  89. через переведення рядка (LF). Переведення рядка представлено десятковим кодом 10,
  90. та шістнадцятковим 0x0A.
  91. </para>
  92. <para>
  93. Примітка: Не використовувати повернення каретки (CR) у відповідності до ОС Apple (0x0D) або
  94. комбінацію повернення каретки - переведення рядка (CRLF)
  95. що є стандартом для ОС Windows (0x0D, 0x0A).
  96. </para>
  97. </sect2>
  98. </sect1>
  99. <sect1 id="coding-standard.naming-conventions">
  100. <title>Принципи іменування</title>
  101. <sect2 id="coding-standard.naming-conventions.classes">
  102. <title>Класи</title>
  103. <para>
  104. Zend Framework standardizes on a class naming convention whereby the names of the
  105. classes directly map to the directories in which they are stored. The root level
  106. directory of Zend Framework's standard library is the "Zend/" directory, whereas
  107. the root level directory of Zend Framework's extras library is the "ZendX/"
  108. directory. All Zend Framework classes are stored hierarchically under these root
  109. directories..
  110. </para>
  111. <para>
  112. Class names may only contain alphanumeric characters. Numbers are permitted
  113. in class names but are discouraged in most cases. Underscores are only permitted in
  114. place of the path separator; the filename "<filename>Zend/Db/Table.php</filename>"
  115. must map to the class name "<classname>Zend_Db_Table</classname>".
  116. </para>
  117. <para>
  118. If a class name is comprised of more than one word, the first letter of each new
  119. word must be capitalized. Successive capitalized letters are not allowed, e.g.
  120. a class "Zend_PDF" is not allowed while "<classname>Zend_Pdf</classname>" is
  121. acceptable.
  122. </para>
  123. <para>
  124. These conventions define a pseudo-namespace mechanism for Zend Framework. Zend
  125. Framework will adopt the <acronym>PHP</acronym> namespace feature when it becomes
  126. available and is feasible for our developers to use in their applications.
  127. </para>
  128. <para>
  129. See the class names in the standard and extras libraries for examples of this
  130. classname convention.
  131. </para>
  132. <note>
  133. <para>
  134. <emphasis>Important</emphasis>: Code that must be deployed alongside
  135. Zend Framework libraries but is not part of the standard or extras libraries
  136. (e.g. application code or libraries that are not distributed by Zend) must
  137. never start with "Zend_" or "ZendX_".
  138. </para>
  139. </note>
  140. </sect2>
  141. <sect2 id="coding-standard.naming-conventions.abstracts">
  142. <title>Abstract Classes</title>
  143. <para>
  144. In general, abstract classes follow the same conventions as <link
  145. linkend="coding-standard.naming-conventions.classes">classes</link>,
  146. with one additional rule: abstract class names must end in the term, "Abstract",
  147. and that term must not be preceded by an underscore. As an example,
  148. <classname>Zend_Controller_Plugin_Abstract</classname> is considered an
  149. invalid name, but <classname>Zend_Controller_PluginAbstract</classname> or
  150. <classname>Zend_Controller_Plugin_PluginAbstract</classname> would be valid
  151. names.
  152. </para>
  153. <note>
  154. <para>
  155. This naming convention is new with version 1.9.0 of Zend Framework. Classes
  156. that pre-date that version may not follow this rule, but will be renamed in
  157. the future in order to comply.
  158. </para>
  159. </note>
  160. </sect2>
  161. <sect2 id="coding-standard.naming-conventions.interfaces">
  162. <title>Interfaces</title>
  163. <para>
  164. In general, interfaces follow the same conventions as <link
  165. linkend="coding-standard.naming-conventions.classes">classes</link>,
  166. with one additional rule: interface names may optionally end in the term,
  167. "Interface", but that term must not be preceded by an underscore. As an example,
  168. <classname>Zend_Controller_Plugin_Interface</classname> is considered an
  169. invalid name, but <classname>Zend_Controller_PluginInterface</classname> or
  170. <classname>Zend_Controller_Plugin_PluginInterface</classname> would be valid
  171. names.
  172. </para>
  173. <para>
  174. While this rule is not required, it is strongly recommended, as it provides a
  175. good visual cue to developers as to which files contain interfaces rather than
  176. classes.
  177. </para>
  178. <note>
  179. <para>
  180. This naming convention is new with version 1.9.0 of Zend Framework. Classes
  181. that pre-date that version may not follow this rule, but will be renamed in
  182. the future in order to comply.
  183. </para>
  184. </note>
  185. </sect2>
  186. <sect2 id="coding-standard.naming-conventions.filenames">
  187. <title>Filenames</title>
  188. <para>
  189. For all other files, only alphanumeric characters, underscores, and the dash
  190. character ("-") are permitted. Spaces are strictly prohibited.
  191. </para>
  192. <para>
  193. Any file that contains <acronym>PHP</acronym> code should end with the extension
  194. "<filename>.php</filename>", with the notable exception of view scripts. The
  195. following examples show acceptable filenames for Zend Framework classes:
  196. </para>
  197. <programlisting language="php"><![CDATA[
  198. Zend/Db.php
  199. Zend/Controller/Front.php
  200. Zend/View/Helper/FormRadio.php
  201. ]]></programlisting>
  202. <para>
  203. File names must map to class names as described above.
  204. </para>
  205. </sect2>
  206. <sect2 id="coding-standard.naming-conventions.functions-and-methods">
  207. <title>Functions and Methods</title>
  208. <para>
  209. Function names may only contain alphanumeric characters. Underscores are not
  210. permitted. Numbers are permitted in function names but are discouraged in most
  211. cases.
  212. </para>
  213. <para>
  214. Function names must always start with a lowercase letter. When a function name
  215. consists of more than one word, the first letter of each new word must be
  216. capitalized. This is commonly called "camelCase" formatting.
  217. </para>
  218. <para>
  219. Verbosity is generally encouraged. Function names should be as verbose as is
  220. practical to fully describe their purpose and behavior.
  221. </para>
  222. <para>
  223. These are examples of acceptable names for functions:
  224. </para>
  225. <programlisting language="php"><![CDATA[
  226. filterInput()
  227. getElementById()
  228. widgetFactory()
  229. ]]></programlisting>
  230. <para>
  231. For object-oriented programming, accessors for instance or static variables should
  232. always be prefixed with "get" or "set". In implementing design patterns, such as the
  233. singleton or factory patterns, the name of the method should contain the pattern
  234. name where practical to more thoroughly describe behavior.
  235. </para>
  236. <para>
  237. For methods on objects that are declared with the "private" or "protected" modifier,
  238. the first character of the method name must be an underscore. This is the only
  239. acceptable application of an underscore in a method name. Methods declared "public"
  240. should never contain an underscore.
  241. </para>
  242. <para>
  243. Functions in the global scope (a.k.a "floating functions") are permitted but
  244. discouraged in most cases. Consider wrapping these functions in a static class.
  245. </para>
  246. </sect2>
  247. <sect2 id="coding-standard.naming-conventions.variables">
  248. <title>Variables</title>
  249. <para>
  250. Variable names may only contain alphanumeric characters. Underscores are not
  251. permitted. Numbers are permitted in variable names but are discouraged in most
  252. cases.
  253. </para>
  254. <para>
  255. For instance variables that are declared with the "private" or "protected" modifier,
  256. the first character of the variable name must be a single underscore. This is the
  257. only acceptable application of an underscore in a variable name. Member variables
  258. declared "public" should never start with an underscore.
  259. </para>
  260. <para>
  261. As with function names (see section 3.3) variable names must always start with a
  262. lowercase letter and follow the "camelCaps" capitalization convention.
  263. </para>
  264. <para>
  265. Verbosity is generally encouraged. Variables should always be as verbose as
  266. practical to describe the data that the developer intends to store in them. Terse
  267. variable names such as "<varname>$i</varname>" and "<varname>$n</varname>" are
  268. discouraged for all but the smallest loop contexts. If a loop contains more than
  269. 20 lines of code, the index variables should have more descriptive names.
  270. </para>
  271. </sect2>
  272. <sect2 id="coding-standard.naming-conventions.constants">
  273. <title>Constants</title>
  274. <para>
  275. Constants may contain both alphanumeric characters and underscores. Numbers are
  276. permitted in constant names.
  277. </para>
  278. <para>
  279. All letters used in a constant name must be capitalized, while all words in a
  280. constant name must be separated by underscore characters.
  281. </para>
  282. <para>
  283. For example, <constant>EMBED_SUPPRESS_EMBED_EXCEPTION</constant> is permitted but
  284. <constant>EMBED_SUPPRESSEMBEDEXCEPTION</constant> is not.
  285. </para>
  286. <para>
  287. Constants must be defined as class members with the "const" modifier. Defining
  288. constants in the global scope with the "define" function is permitted but strongly
  289. discouraged.
  290. </para>
  291. </sect2>
  292. </sect1>
  293. <sect1 id="coding-standard.coding-style">
  294. <title>Coding Style</title>
  295. <sect2 id="coding-standard.coding-style.php-code-demarcation">
  296. <title>PHP Code Demarcation</title>
  297. <para>
  298. <acronym>PHP</acronym> code must always be delimited by the full-form, standard
  299. <acronym>PHP</acronym> tags:
  300. </para>
  301. <programlisting language="php"><![CDATA[
  302. <?php
  303. ?>
  304. ]]></programlisting>
  305. <para>
  306. Short tags are never allowed. For files containing only <acronym>PHP</acronym>
  307. code, the closing tag must always be omitted (See <link
  308. linkend="coding-standard.php-file-formatting.general">General standards</link>).
  309. </para>
  310. </sect2>
  311. <sect2 id="coding-standard.coding-style.strings">
  312. <title>Strings</title>
  313. <sect3 id="coding-standard.coding-style.strings.literals">
  314. <title>String Literals</title>
  315. <para>
  316. When a string is literal (contains no variable substitutions), the apostrophe or
  317. "single quote" should always be used to demarcate the string:
  318. </para>
  319. <programlisting language="php"><![CDATA[
  320. $a = 'Example String';
  321. ]]></programlisting>
  322. </sect3>
  323. <sect3 id="coding-standard.coding-style.strings.literals-containing-apostrophes">
  324. <title>String Literals Containing Apostrophes</title>
  325. <para>
  326. When a literal string itself contains apostrophes, it is permitted to demarcate
  327. the string with quotation marks or "double quotes". This is especially useful
  328. for <constant>SQL</constant> statements:
  329. </para>
  330. <programlisting language="php"><![CDATA[
  331. $sql = "SELECT `id`, `name` from `people` "
  332. . "WHERE `name`='Fred' OR `name`='Susan'";
  333. ]]></programlisting>
  334. <para>
  335. This syntax is preferred over escaping apostrophes as it is much easier to read.
  336. </para>
  337. </sect3>
  338. <sect3 id="coding-standard.coding-style.strings.variable-substitution">
  339. <title>Variable Substitution</title>
  340. <para>
  341. Variable substitution is permitted using either of these forms:
  342. </para>
  343. <programlisting language="php"><![CDATA[
  344. $greeting = "Hello $name, welcome back!";
  345. $greeting = "Hello {$name}, welcome back!";
  346. ]]></programlisting>
  347. <para>
  348. For consistency, this form is not permitted:
  349. </para>
  350. <programlisting language="php"><![CDATA[
  351. $greeting = "Hello ${name}, welcome back!";
  352. ]]></programlisting>
  353. </sect3>
  354. <sect3 id="coding-standard.coding-style.strings.string-concatenation">
  355. <title>String Concatenation</title>
  356. <para>
  357. Strings must be concatenated using the "." operator. A space must always
  358. be added before and after the "." operator to improve readability:
  359. </para>
  360. <programlisting language="php"><![CDATA[
  361. $company = 'Zend' . ' ' . 'Technologies';
  362. ]]></programlisting>
  363. <para>
  364. When concatenating strings with the "." operator, it is encouraged to
  365. break the statement into multiple lines to improve readability. In these
  366. cases, each successive line should be padded with white space such that the
  367. "."; operator is aligned under the "=" operator:
  368. </para>
  369. <programlisting language="php"><![CDATA[
  370. $sql = "SELECT `id`, `name` FROM `people` "
  371. . "WHERE `name` = 'Susan' "
  372. . "ORDER BY `name` ASC ";
  373. ]]></programlisting>
  374. </sect3>
  375. </sect2>
  376. <sect2 id="coding-standard.coding-style.arrays">
  377. <title>Arrays</title>
  378. <sect3 id="coding-standard.coding-style.arrays.numerically-indexed">
  379. <title>Numerically Indexed Arrays</title>
  380. <para>Negative numbers are not permitted as indices.</para>
  381. <para>
  382. An indexed array may start with any non-negative number, however
  383. all base indices besides 0 are discouraged.
  384. </para>
  385. <para>
  386. When declaring indexed arrays with the <type>Array</type> function, a trailing
  387. space must be added after each comma delimiter to improve readability:
  388. </para>
  389. <programlisting language="php"><![CDATA[
  390. $sampleArray = array(1, 2, 3, 'Zend', 'Studio');
  391. ]]></programlisting>
  392. <para>
  393. It is permitted to declare multi-line indexed arrays using the "array"
  394. construct. In this case, each successive line must be padded with spaces such
  395. that beginning of each line is aligned:
  396. </para>
  397. <programlisting language="php"><![CDATA[
  398. $sampleArray = array(1, 2, 3, 'Zend', 'Studio',
  399. $a, $b, $c,
  400. 56.44, $d, 500);
  401. ]]></programlisting>
  402. <para>
  403. Alternately, the initial array item may begin on the following line. If so,
  404. it should be padded at one indentation level greater than the line containing
  405. the array declaration, and all successive lines should have the same
  406. indentation; the closing paren should be on a line by itself at the same
  407. indentation level as the line containing the array declaration:
  408. </para>
  409. <programlisting language="php"><![CDATA[
  410. $sampleArray = array(
  411. 1, 2, 3, 'Zend', 'Studio',
  412. $a, $b, $c,
  413. 56.44, $d, 500,
  414. );
  415. ]]></programlisting>
  416. <para>
  417. When using this latter declaration, we encourage using a trailing comma for
  418. the last item in the array; this minimizes the impact of adding new items on
  419. successive lines, and helps to ensure no parse errors occur due to a missing
  420. comma.
  421. </para>
  422. </sect3>
  423. <sect3 id="coding-standard.coding-style.arrays.associative">
  424. <title>Associative Arrays</title>
  425. <para>
  426. When declaring associative arrays with the <type>Array</type> construct,
  427. breaking the statement into multiple lines is encouraged. In this case, each
  428. successive line must be padded with white space such that both the keys and the
  429. values are aligned:
  430. </para>
  431. <programlisting language="php"><![CDATA[
  432. $sampleArray = array('firstKey' => 'firstValue',
  433. 'secondKey' => 'secondValue');
  434. ]]></programlisting>
  435. <para>
  436. Alternately, the initial array item may begin on the following line. If so,
  437. it should be padded at one indentation level greater than the line containing
  438. the array declaration, and all successive lines should have the same
  439. indentation; the closing paren should be on a line by itself at the same
  440. indentation level as the line containing the array declaration. For
  441. readability, the various "=>" assignment operators should be padded such that
  442. they align.
  443. </para>
  444. <programlisting language="php"><![CDATA[
  445. $sampleArray = array(
  446. 'firstKey' => 'firstValue',
  447. 'secondKey' => 'secondValue',
  448. );
  449. ]]></programlisting>
  450. <para>
  451. When using this latter declaration, we encourage using a trailing comma for
  452. the last item in the array; this minimizes the impact of adding new items on
  453. successive lines, and helps to ensure no parse errors occur due to a missing
  454. comma.
  455. </para>
  456. </sect3>
  457. </sect2>
  458. <sect2 id="coding-standard.coding-style.classes">
  459. <title>Classes</title>
  460. <sect3 id="coding-standard.coding-style.classes.declaration">
  461. <title>Class Declaration</title>
  462. <para>
  463. Classes must be named according to Zend Framework's naming conventions.
  464. </para>
  465. <para>
  466. The brace should always be written on the line underneath the class name.
  467. </para>
  468. <para>
  469. Every class must have a documentation block that conforms to the PHPDocumentor
  470. standard.
  471. </para>
  472. <para>
  473. All code in a class must be indented with four spaces.
  474. </para>
  475. <para>
  476. Only one class is permitted in each <acronym>PHP</acronym> file.
  477. </para>
  478. <para>
  479. Placing additional code in class files is permitted but discouraged.
  480. In such files, two blank lines must separate the class from any additional
  481. <acronym>PHP</acronym> code in the class file.
  482. </para>
  483. <para>
  484. The following is an example of an acceptable class declaration:
  485. </para>
  486. <programlisting language="php"><![CDATA[
  487. /**
  488. * Documentation Block Here
  489. */
  490. class SampleClass
  491. {
  492. // all contents of class
  493. // must be indented four spaces
  494. }
  495. ]]></programlisting>
  496. <para>
  497. Classes that extend other classes or which implement interfaces should
  498. declare their dependencies on the same line when possible.
  499. </para>
  500. <programlisting language="php"><![CDATA[
  501. class SampleClass extends FooAbstract implements BarInterface
  502. {
  503. }
  504. ]]></programlisting>
  505. <para>
  506. If as a result of such declarations, the line length exceeds the <link
  507. linkend="coding-standard.php-file-formatting.max-line-length">maximum line
  508. length</link>, break the line before the "extends" and/or "implements"
  509. keywords, and pad those lines by one indentation level.
  510. </para>
  511. <programlisting language="php"><![CDATA[
  512. class SampleClass
  513. extends FooAbstract
  514. implements BarInterface
  515. {
  516. }
  517. ]]></programlisting>
  518. <para>
  519. If the class implements multiple interfaces and the declaration exceeds the
  520. maximum line length, break after each comma separating the interfaces, and
  521. indent the interface names such that they align.
  522. </para>
  523. <programlisting language="php"><![CDATA[
  524. class SampleClass
  525. implements BarInterface,
  526. BazInterface
  527. {
  528. }
  529. ]]></programlisting>
  530. </sect3>
  531. <sect3 id="coding-standard.coding-style.classes.member-variables">
  532. <title>Class Member Variables</title>
  533. <para>
  534. Member variables must be named according to Zend Framework's variable naming
  535. conventions.
  536. </para>
  537. <para>
  538. Any variables declared in a class must be listed at the top of the class, above
  539. the declaration of any methods.
  540. </para>
  541. <para>
  542. The <emphasis>var</emphasis> construct is not permitted. Member variables always
  543. declare their visibility by using one of the <property>private</property>,
  544. <property>protected</property>, or <property>public</property> modifiers. Giving
  545. access to member variables directly by declaring them as public is permitted but
  546. discouraged in favor of accessor methods (set &amp; get).
  547. </para>
  548. </sect3>
  549. </sect2>
  550. <sect2 id="coding-standard.coding-style.functions-and-methods">
  551. <title>Functions and Methods</title>
  552. <sect3 id="coding-standard.coding-style.functions-and-methods.declaration">
  553. <title>Function and Method Declaration</title>
  554. <para>
  555. Functions must be named according to Zend Framework's function naming
  556. conventions.
  557. </para>
  558. <para>
  559. Methods inside classes must always declare their visibility by using
  560. one of the <property>private</property>, <property>protected</property>,
  561. or <property>public</property> modifiers.
  562. </para>
  563. <para>
  564. As with classes, the brace should always be written on the line underneath the
  565. function name. Space between the function name and the opening parenthesis for
  566. the arguments is not permitted.
  567. </para>
  568. <para>
  569. Functions in the global scope are strongly discouraged.
  570. </para>
  571. <para>
  572. The following is an example of an acceptable function declaration in a class:
  573. </para>
  574. <programlisting language="php"><![CDATA[
  575. /**
  576. * Documentation Block Here
  577. */
  578. class Foo
  579. {
  580. /**
  581. * Documentation Block Here
  582. */
  583. public function bar()
  584. {
  585. // all contents of function
  586. // must be indented four spaces
  587. }
  588. }
  589. ]]></programlisting>
  590. <para>
  591. In cases where the argument list exceeds the <link
  592. linkend="coding-standard.php-file-formatting.max-line-length">maximum line
  593. length</link>, you may introduce line breaks. Additional arguments to the
  594. function or method must be indented one additional level beyond the function
  595. or method declaration. A line break should then occur before the closing
  596. argument paren, which should then be placed on the same line as the opening
  597. brace of the function or method with one space separating the two, and at the
  598. same indentation level as the function or method declaration. The following is
  599. an example of one such situation:
  600. </para>
  601. <programlisting language="php"><![CDATA[
  602. /**
  603. * Documentation Block Here
  604. */
  605. class Foo
  606. {
  607. /**
  608. * Documentation Block Here
  609. */
  610. public function bar($arg1, $arg2, $arg3,
  611. $arg4, $arg5, $arg6
  612. ) {
  613. // all contents of function
  614. // must be indented four spaces
  615. }
  616. }
  617. ]]></programlisting>
  618. <note>
  619. <para>
  620. <emphasis>Note</emphasis>: Pass-by-reference is the only parameter passing
  621. mechanism permitted in a method declaration.
  622. </para>
  623. </note>
  624. <programlisting language="php"><![CDATA[
  625. /**
  626. * Documentation Block Here
  627. */
  628. class Foo
  629. {
  630. /**
  631. * Documentation Block Here
  632. */
  633. public function bar(&$baz)
  634. {}
  635. }
  636. ]]></programlisting>
  637. <para>
  638. Call-time pass-by-reference is strictly prohibited.
  639. </para>
  640. <para>
  641. The return value must not be enclosed in parentheses. This can hinder
  642. readability, in additional to breaking code if a method is later changed to
  643. return by reference.
  644. </para>
  645. <programlisting language="php"><![CDATA[
  646. /**
  647. * Documentation Block Here
  648. */
  649. class Foo
  650. {
  651. /**
  652. * WRONG
  653. */
  654. public function bar()
  655. {
  656. return($this->bar);
  657. }
  658. /**
  659. * RIGHT
  660. */
  661. public function bar()
  662. {
  663. return $this->bar;
  664. }
  665. }
  666. ]]></programlisting>
  667. </sect3>
  668. <sect3 id="coding-standard.coding-style.functions-and-methods.usage">
  669. <title>Function and Method Usage</title>
  670. <para>
  671. Function arguments should be separated by a single trailing space after the
  672. comma delimiter. The following is an example of an acceptable invocation of a
  673. function that takes three arguments:
  674. </para>
  675. <programlisting language="php"><![CDATA[
  676. threeArguments(1, 2, 3);
  677. ]]></programlisting>
  678. <para>
  679. Call-time pass-by-reference is strictly prohibited. See the function
  680. declarations section for the proper way to pass function arguments by-reference.
  681. </para>
  682. <para>
  683. In passing arrays as arguments to a function, the function call may include the
  684. "array" hint and may be split into multiple lines to improve readability. In
  685. such cases, the normal guidelines for writing arrays still apply:
  686. </para>
  687. <programlisting language="php"><![CDATA[
  688. threeArguments(array(1, 2, 3), 2, 3);
  689. threeArguments(array(1, 2, 3, 'Zend', 'Studio',
  690. $a, $b, $c,
  691. 56.44, $d, 500), 2, 3);
  692. threeArguments(array(
  693. 1, 2, 3, 'Zend', 'Studio',
  694. $a, $b, $c,
  695. 56.44, $d, 500
  696. ), 2, 3);
  697. ]]></programlisting>
  698. </sect3>
  699. </sect2>
  700. <sect2 id="coding-standard.coding-style.control-statements">
  701. <title>Control Statements</title>
  702. <sect3 id="coding-standard.coding-style.control-statements.if-else-elseif">
  703. <title>If/Else/Elseif</title>
  704. <para>
  705. Control statements based on the <emphasis>if</emphasis> and
  706. <emphasis>elseif</emphasis> constructs must have a single space before the
  707. opening parenthesis of the conditional and a single space after the closing
  708. parenthesis.
  709. </para>
  710. <para>
  711. Within the conditional statements between the parentheses, operators must be
  712. separated by spaces for readability. Inner parentheses are encouraged to improve
  713. logical grouping for larger conditional expressions.
  714. </para>
  715. <para>
  716. The opening brace is written on the same line as the conditional statement. The
  717. closing brace is always written on its own line. Any content within the braces
  718. must be indented using four spaces.
  719. </para>
  720. <programlisting language="php"><![CDATA[
  721. if ($a != 2) {
  722. $a = 2;
  723. }
  724. ]]></programlisting>
  725. <para>
  726. If the conditional statement causes the line length to exceed the <link
  727. linkend="coding-standard.php-file-formatting.max-line-length">maximum line
  728. length</link> and has several clauses, you may break the conditional into
  729. multiple lines. In such a case, break the line prior to a logic operator, and
  730. pad the line such that it aligns under the first character of the conditional
  731. clause. The closing paren in the conditional will then be placed on a line with
  732. the opening brace, with one space separating the two, at an indentation level
  733. equivalent to the opening control statement.
  734. </para>
  735. <programlisting language="php"><![CDATA[
  736. if (($a == $b)
  737. && ($b == $c)
  738. || (Foo::CONST == $d)
  739. ) {
  740. $a = $d;
  741. }
  742. ]]></programlisting>
  743. <para>
  744. The intention of this latter declaration format is to prevent issues when
  745. adding or removing clauses from the conditional during later revisions.
  746. </para>
  747. <para>
  748. For "if" statements that include "elseif" or "else", the formatting conventions
  749. are similar to the "if" construct. The following examples demonstrate proper
  750. formatting for "if" statements with "else" and/or "elseif" constructs:
  751. </para>
  752. <programlisting language="php"><![CDATA[
  753. if ($a != 2) {
  754. $a = 2;
  755. } else {
  756. $a = 7;
  757. }
  758. if ($a != 2) {
  759. $a = 2;
  760. } elseif ($a == 3) {
  761. $a = 4;
  762. } else {
  763. $a = 7;
  764. }
  765. if (($a == $b)
  766. && ($b == $c)
  767. || (Foo::CONST == $d)
  768. ) {
  769. $a = $d;
  770. } elseif (($a != $b)
  771. || ($b != $c)
  772. ) {
  773. $a = $c;
  774. } else {
  775. $a = $b;
  776. }
  777. ]]></programlisting>
  778. <para>
  779. <acronym>PHP</acronym> allows statements to be written without braces in some
  780. circumstances. This coding standard makes no differentiation- all "if",
  781. "elseif" or "else" statements must use braces.
  782. </para>
  783. </sect3>
  784. <sect3 id="coding-standards.coding-style.control-statements.switch">
  785. <title>Switch</title>
  786. <para>
  787. Control statements written with the "switch" statement must have a single space
  788. before the opening parenthesis of the conditional statement and after the
  789. closing parenthesis.
  790. </para>
  791. <para>
  792. All content within the "switch" statement must be indented using four spaces.
  793. Content under each "case" statement must be indented using an additional four
  794. spaces.
  795. </para>
  796. <programlisting language="php"><![CDATA[
  797. switch ($numPeople) {
  798. case 1:
  799. break;
  800. case 2:
  801. break;
  802. default:
  803. break;
  804. }
  805. ]]></programlisting>
  806. <para>
  807. The construct <property>default</property> should never be omitted from a
  808. <property>switch</property> statement.
  809. </para>
  810. <note>
  811. <para>
  812. <emphasis>Note</emphasis>: It is sometimes useful to write a
  813. <property>case</property> statement which falls through to the next case by
  814. not including a <property>break</property> or <property>return</property>
  815. within that case. To distinguish these cases from bugs, any
  816. <property>case</property> statement where <property>break</property> or
  817. <property>return</property> are omitted should contain a comment indicating
  818. that the break was intentionally omitted.
  819. </para>
  820. </note>
  821. </sect3>
  822. </sect2>
  823. <sect2 id="coding-standards.inline-documentation">
  824. <title>Inline Documentation</title>
  825. <sect3 id="coding-standards.inline-documentation.documentation-format">
  826. <title>Documentation Format</title>
  827. <para>
  828. All documentation blocks ("docblocks") must be compatible with the phpDocumentor
  829. format. Describing the phpDocumentor format is beyond the scope of this
  830. document. For more information, visit: <ulink
  831. url="http://phpdoc.org/">http://phpdoc.org/</ulink>
  832. </para>
  833. <para>
  834. All class files must contain a "file-level" docblock at the top of each file and
  835. a "class-level" docblock immediately above each class. Examples of such
  836. docblocks can be found below.
  837. </para>
  838. </sect3>
  839. <sect3 id="coding-standards.inline-documentation.files">
  840. <title>Files</title>
  841. <para>
  842. Every file that contains <acronym>PHP</acronym> code must have a docblock at
  843. the top of the file that contains these phpDocumentor tags at a minimum:
  844. </para>
  845. <programlisting language="php"><![CDATA[
  846. /**
  847. * Short description for file
  848. *
  849. * Long description for file (if any)...
  850. *
  851. * LICENSE: Some license information
  852. *
  853. * @category Zend
  854. * @package Zend_Magic
  855. * @subpackage Wand
  856. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  857. * @license http://framework.zend.com/license BSD License
  858. * @version $Id:$
  859. * @link http://framework.zend.com/package/PackageName
  860. * @since File available since Release 1.5.0
  861. */
  862. ]]></programlisting>
  863. <para>
  864. The <property>@category</property> annotation must have a value of "Zend".
  865. </para>
  866. <para>
  867. The <property>@package</property> annotation must be assigned, and should be
  868. equivalent to the component name of the class contained in the file; typically,
  869. this will only have two segments, the "Zend" prefix, and the component name.
  870. </para>
  871. <para>
  872. The <property>@subpackage</property> annotation is optional. If provided, it
  873. should be the subcomponent name, minus the class prefix. In the example above,
  874. the assumption is that the class in the file is either
  875. "<classname>Zend_Magic_Wand</classname>", or uses that classname as part of its
  876. prefix.
  877. </para>
  878. </sect3>
  879. <sect3 id="coding-standards.inline-documentation.classes">
  880. <title>Classes</title>
  881. <para>
  882. Every class must have a docblock that contains these phpDocumentor tags at a
  883. minimum:
  884. </para>
  885. <programlisting language="php"><![CDATA[
  886. /**
  887. * Short description for class
  888. *
  889. * Long description for class (if any)...
  890. *
  891. * @category Zend
  892. * @package Zend_Magic
  893. * @subpackage Wand
  894. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  895. * @license http://framework.zend.com/license BSD License
  896. * @version Release: @package_version@
  897. * @link http://framework.zend.com/package/PackageName
  898. * @since Class available since Release 1.5.0
  899. * @deprecated Class deprecated in Release 2.0.0
  900. */
  901. ]]></programlisting>
  902. <para>
  903. The <property>@category</property> annotation must have a value of "Zend".
  904. </para>
  905. <para>
  906. The <property>@package</property> annotation must be assigned, and should be
  907. equivalent to the component to which the class belongs; typically, this will
  908. only have two segments, the "Zend" prefix, and the component name.
  909. </para>
  910. <para>
  911. The <property>@subpackage</property> annotation is optional. If provided, it
  912. should be the subcomponent name, minus the class prefix. In the example above,
  913. the assumption is that the class described is either
  914. "<classname>Zend_Magic_Wand</classname>", or uses that classname as part of its
  915. prefix.
  916. </para>
  917. </sect3>
  918. <sect3 id="coding-standards.inline-documentation.functions">
  919. <title>Functions</title>
  920. <para>
  921. Every function, including object methods, must have a docblock that contains at
  922. a minimum:
  923. </para>
  924. <itemizedlist>
  925. <listitem><para>A description of the function</para></listitem>
  926. <listitem><para>All of the arguments</para></listitem>
  927. <listitem><para>All of the possible return values</para></listitem>
  928. </itemizedlist>
  929. <para>
  930. It is not necessary to use the "@access" tag because the access level is already
  931. known from the "public", "private", or "protected" modifier used to declare the
  932. function.
  933. </para>
  934. <para>
  935. If a function or method may throw an exception, use @throws for all known
  936. exception classes:
  937. </para>
  938. <programlisting language="php"><![CDATA[
  939. @throws exceptionclass [description]
  940. ]]></programlisting>
  941. </sect3>
  942. </sect2>
  943. </sect1>
  944. </appendix>
  945. <!--
  946. vim:se ts=4 sw=4 et:
  947. -->