coding_standard.xml 41 KB

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