coding_standard.xml 41 KB

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