coding_standard.xml 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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.filenames">
  137. <title>Filenames</title>
  138. <para>
  139. For all other files, only alphanumeric characters, underscores, and the dash
  140. character ("-") are permitted. Spaces are strictly prohibited.
  141. </para>
  142. <para>
  143. Any file that contains <acronym>PHP</acronym> code should end with the extension
  144. "<filename>.php</filename>", with the notable exception of view scripts. The
  145. following examples show acceptable filenames for Zend Framework classes:
  146. </para>
  147. <programlisting language="php"><![CDATA[
  148. Zend/Db.php
  149. Zend/Controller/Front.php
  150. Zend/View/Helper/FormRadio.php
  151. ]]></programlisting>
  152. <para>
  153. File names must map to class names as described above.
  154. </para>
  155. </sect2>
  156. <sect2 id="coding-standard.naming-conventions.functions-and-methods">
  157. <title>Functions and Methods</title>
  158. <para>
  159. Function names may only contain alphanumeric characters. Underscores are not
  160. permitted. Numbers are permitted in function names but are discouraged in most
  161. cases.
  162. </para>
  163. <para>
  164. Function names must always start with a lowercase letter. When a function name
  165. consists of more than one word, the first letter of each new word must be
  166. capitalized. This is commonly called "camelCase" formatting.
  167. </para>
  168. <para>
  169. Verbosity is generally encouraged. Function names should be as verbose as is
  170. practical to fully describe their purpose and behavior.
  171. </para>
  172. <para>
  173. These are examples of acceptable names for functions:
  174. </para>
  175. <programlisting language="php"><![CDATA[
  176. filterInput()
  177. getElementById()
  178. widgetFactory()
  179. ]]></programlisting>
  180. <para>
  181. For object-oriented programming, accessors for instance or static variables should
  182. always be prefixed with "get" or "set". In implementing design patterns, such as the
  183. singleton or factory patterns, the name of the method should contain the pattern
  184. name where practical to more thoroughly describe behavior.
  185. </para>
  186. <para>
  187. For methods on objects that are declared with the "private" or "protected" modifier,
  188. the first character of the method name must be an underscore. This is the only
  189. acceptable application of an underscore in a method name. Methods declared "public"
  190. should never contain an underscore.
  191. </para>
  192. <para>
  193. Functions in the global scope (a.k.a "floating functions") are permitted but
  194. discouraged in most cases. Consider wrapping these functions in a static class.
  195. </para>
  196. </sect2>
  197. <sect2 id="coding-standard.naming-conventions.variables">
  198. <title>Variables</title>
  199. <para>
  200. Variable names may only contain alphanumeric characters. Underscores are not
  201. permitted. Numbers are permitted in variable names but are discouraged in most
  202. cases.
  203. </para>
  204. <para>
  205. For instance variables that are declared with the "private" or "protected" modifier,
  206. the first character of the variable name must be a single underscore. This is the
  207. only acceptable application of an underscore in a variable name. Member variables
  208. declared "public" should never start with an underscore.
  209. </para>
  210. <para>
  211. As with function names (see section 3.3) variable names must always start with a
  212. lowercase letter and follow the "camelCaps" capitalization convention.
  213. </para>
  214. <para>
  215. Verbosity is generally encouraged. Variables should always be as verbose as
  216. practical to describe the data that the developer intends to store in them. Terse
  217. variable names such as "$i" and "$n" are discouraged for all but the smallest loop
  218. contexts. If a loop contains more than 20 lines of code, the index variables should
  219. have more descriptive names.
  220. </para>
  221. </sect2>
  222. <sect2 id="coding-standard.naming-conventions.constants">
  223. <title>Constants</title>
  224. <para>
  225. Constants may contain both alphanumeric characters and underscores. Numbers are
  226. permitted in constant names.
  227. </para>
  228. <para>
  229. All letters used in a constant name must be capitalized, while all words in a
  230. constant name must be separated by underscore characters.
  231. </para>
  232. <para>
  233. For example, <constant>EMBED_SUPPRESS_EMBED_EXCEPTION</constant> is permitted but
  234. <constant>EMBED_SUPPRESSEMBEDEXCEPTION</constant> is not.
  235. </para>
  236. <para>
  237. Constants must be defined as class members with the "const" modifier. Defining
  238. constants in the global scope with the "define" function is permitted but strongly
  239. discouraged.
  240. </para>
  241. </sect2>
  242. </sect1>
  243. <sect1 id="coding-standard.coding-style">
  244. <title>Coding Style</title>
  245. <sect2 id="coding-standard.coding-style.php-code-demarcation">
  246. <title>PHP Code Demarcation</title>
  247. <para>
  248. <acronym>PHP</acronym> code must always be delimited by the full-form, standard
  249. <acronym>PHP</acronym> tags:
  250. </para>
  251. <programlisting language="php"><![CDATA[
  252. <?php
  253. ?>
  254. ]]></programlisting>
  255. <para>
  256. Short tags are never allowed. For files containing only <acronym>PHP</acronym>
  257. code, the closing tag must always be omitted (See <xref
  258. linkend="coding-standard.php-file-formatting.general" />).
  259. </para>
  260. </sect2>
  261. <sect2 id="coding-standard.coding-style.strings">
  262. <title>Strings</title>
  263. <sect3 id="coding-standard.coding-style.strings.literals">
  264. <title>String Literals</title>
  265. <para>
  266. When a string is literal (contains no variable substitutions), the apostrophe or
  267. "single quote" should always be used to demarcate the string:
  268. </para>
  269. <programlisting language="php"><![CDATA[
  270. $a = 'Example String';
  271. ]]></programlisting>
  272. </sect3>
  273. <sect3 id="coding-standard.coding-style.strings.literals-containing-apostrophes">
  274. <title>String Literals Containing Apostrophes</title>
  275. <para>
  276. When a literal string itself contains apostrophes, it is permitted to demarcate
  277. the string with quotation marks or "double quotes". This is especially useful
  278. for <constant>SQL</constant> statements:
  279. </para>
  280. <programlisting language="php"><![CDATA[
  281. $sql = "SELECT `id`, `name` from `people` "
  282. . "WHERE `name`='Fred' OR `name`='Susan'";
  283. ]]></programlisting>
  284. <para>
  285. This syntax is preferred over escaping apostrophes as it is much easier to read.
  286. </para>
  287. </sect3>
  288. <sect3 id="coding-standard.coding-style.strings.variable-substitution">
  289. <title>Variable Substitution</title>
  290. <para>
  291. Variable substitution is permitted using either of these forms:
  292. </para>
  293. <programlisting language="php"><![CDATA[
  294. $greeting = "Hello $name, welcome back!";
  295. $greeting = "Hello {$name}, welcome back!";
  296. ]]></programlisting>
  297. <para>
  298. For consistency, this form is not permitted:
  299. </para>
  300. <programlisting language="php"><![CDATA[
  301. $greeting = "Hello ${name}, welcome back!";
  302. ]]></programlisting>
  303. </sect3>
  304. <sect3 id="coding-standard.coding-style.strings.string-concatenation">
  305. <title>String Concatenation</title>
  306. <para>
  307. Strings must be concatenated using the "." operator. A space must always
  308. be added before and after the "." operator to improve readability:
  309. </para>
  310. <programlisting language="php"><![CDATA[
  311. $company = 'Zend' . ' ' . 'Technologies';
  312. ]]></programlisting>
  313. <para>
  314. When concatenating strings with the "." operator, it is encouraged to
  315. break the statement into multiple lines to improve readability. In these
  316. cases, each successive line should be padded with white space such that the
  317. "."; operator is aligned under the "=" operator:
  318. </para>
  319. <programlisting language="php"><![CDATA[
  320. $sql = "SELECT `id`, `name` FROM `people` "
  321. . "WHERE `name` = 'Susan' "
  322. . "ORDER BY `name` ASC ";
  323. ]]></programlisting>
  324. </sect3>
  325. </sect2>
  326. <sect2 id="coding-standard.coding-style.arrays">
  327. <title>Arrays</title>
  328. <sect3 id="coding-standard.coding-style.arrays.numerically-indexed">
  329. <title>Numerically Indexed Arrays</title>
  330. <para>Negative numbers are not permitted as indices.</para>
  331. <para>
  332. An indexed array may start with any non-negative number, however
  333. all base indices besides 0 are discouraged.
  334. </para>
  335. <para>
  336. When declaring indexed arrays with the <type>Array</type> function, a trailing
  337. space must be added after each comma delimiter to improve readability:
  338. </para>
  339. <programlisting language="php"><![CDATA[
  340. $sampleArray = array(1, 2, 3, 'Zend', 'Studio');
  341. ]]></programlisting>
  342. <para>
  343. It is permitted to declare multi-line indexed arrays using the "array"
  344. construct. In this case, each successive line must be padded with spaces such
  345. that beginning of each line is aligned:
  346. </para>
  347. <programlisting language="php"><![CDATA[
  348. $sampleArray = array(1, 2, 3, 'Zend', 'Studio',
  349. $a, $b, $c,
  350. 56.44, $d, 500);
  351. ]]></programlisting>
  352. </sect3>
  353. <sect3 id="coding-standard.coding-style.arrays.associative">
  354. <title>Associative Arrays</title>
  355. <para>
  356. When declaring associative arrays with the <type>Array</type> construct,
  357. breaking the statement into multiple lines is encouraged. In this case, each
  358. successive line must be padded with white space such that both the keys and the
  359. values are aligned:
  360. </para>
  361. <programlisting language="php"><![CDATA[
  362. $sampleArray = array('firstKey' => 'firstValue',
  363. 'secondKey' => 'secondValue');
  364. ]]></programlisting>
  365. </sect3>
  366. </sect2>
  367. <sect2 id="coding-standard.coding-style.classes">
  368. <title>Classes</title>
  369. <sect3 id="coding-standard.coding-style.classes.declaration">
  370. <title>Class Declaration</title>
  371. <para>
  372. Classes must be named according to Zend Framework's naming conventions.
  373. </para>
  374. <para>
  375. The brace should always be written on the line underneath the class name.
  376. </para>
  377. <para>
  378. Every class must have a documentation block that conforms to the PHPDocumentor
  379. standard.
  380. </para>
  381. <para>
  382. All code in a class must be indented with four spaces.
  383. </para>
  384. <para>
  385. Only one class is permitted in each <acronym>PHP</acronym> file.
  386. </para>
  387. <para>
  388. Placing additional code in class files is permitted but discouraged.
  389. In such files, two blank lines must separate the class from any additional
  390. <acronym>PHP</acronym> code in the class file.
  391. </para>
  392. <para>
  393. The following is an example of an acceptable class declaration:
  394. </para>
  395. <programlisting language="php"><![CDATA[
  396. /**
  397. * Documentation Block Here
  398. */
  399. class SampleClass
  400. {
  401. // all contents of class
  402. // must be indented four spaces
  403. }
  404. ]]></programlisting>
  405. </sect3>
  406. <sect3 id="coding-standard.coding-style.classes.member-variables">
  407. <title>Class Member Variables</title>
  408. <para>
  409. Member variables must be named according to Zend Framework's variable naming
  410. conventions.
  411. </para>
  412. <para>
  413. Any variables declared in a class must be listed at the top of the class, above
  414. the declaration of any methods.
  415. </para>
  416. <para>
  417. The <code>var</code> construct is not permitted. Member variables always declare
  418. their visibility by using one of the <code>private</code>,
  419. <code>protected</code>, or <code>public</code> modifiers. Giving access to
  420. member variables directly by declaring them as public is permitted but
  421. discouraged in favor of accessor methods (set &amp; get).
  422. </para>
  423. </sect3>
  424. </sect2>
  425. <sect2 id="coding-standard.coding-style.functions-and-methods">
  426. <title>Functions and Methods</title>
  427. <sect3 id="coding-standard.coding-style.functions-and-methods.declaration">
  428. <title>Function and Method Declaration</title>
  429. <para>
  430. Functions must be named according to the Zend Framework function naming
  431. conventions.
  432. </para>
  433. <para>
  434. Methods inside classes must always declare their visibility by using
  435. one of the <code>private</code>, <code>protected</code>,
  436. or <code>public</code> modifiers.
  437. </para>
  438. <para>
  439. As with classes, the brace should always be written on the line underneath the
  440. function name. Space between the function name and the opening parenthesis for
  441. the arguments is not permitted.
  442. </para>
  443. <para>
  444. Functions in the global scope are strongly discouraged.
  445. </para>
  446. <para>
  447. The following is an example of an acceptable function declaration in a class:
  448. <programlisting language="php"><![CDATA[
  449. /**
  450. * Documentation Block Here
  451. */
  452. class Foo
  453. {
  454. /**
  455. * Documentation Block Here
  456. */
  457. public function bar()
  458. {
  459. // all contents of function
  460. // must be indented four spaces
  461. }
  462. }
  463. ]]></programlisting>
  464. </para>
  465. <note>
  466. <para>
  467. <emphasis>Note</emphasis>: Pass-by-reference is the only parameter passing
  468. mechanism permitted in a method declaration.
  469. </para>
  470. </note>
  471. <programlisting language="php"><![CDATA[
  472. /**
  473. * Documentation Block Here
  474. */
  475. class Foo
  476. {
  477. /**
  478. * Documentation Block Here
  479. */
  480. public function bar(&$baz)
  481. {}
  482. }
  483. ]]></programlisting>
  484. <para>
  485. Call-time pass-by-reference is strictly prohibited.
  486. </para>
  487. <para>
  488. The return value must not be enclosed in parentheses. This can hinder
  489. readability, in additional to breaking code if a method is later changed to
  490. return by reference.
  491. </para>
  492. <programlisting language="php"><![CDATA[
  493. /**
  494. * Documentation Block Here
  495. */
  496. class Foo
  497. {
  498. /**
  499. * WRONG
  500. */
  501. public function bar()
  502. {
  503. return($this->bar);
  504. }
  505. /**
  506. * RIGHT
  507. */
  508. public function bar()
  509. {
  510. return $this->bar;
  511. }
  512. }
  513. ]]></programlisting>
  514. </sect3>
  515. <sect3 id="coding-standard.coding-style.functions-and-methods.usage">
  516. <title>Function and Method Usage</title>
  517. <para>
  518. Function arguments should be separated by a single trailing space after the
  519. comma delimiter. The following is an example of an acceptable invocation of a
  520. function that takes three arguments:
  521. </para>
  522. <programlisting language="php"><![CDATA[
  523. threeArguments(1, 2, 3);
  524. ]]></programlisting>
  525. <para>
  526. Call-time pass-by-reference is strictly prohibited. See the function
  527. declarations section for the proper way to pass function arguments by-reference.
  528. </para>
  529. <para>
  530. In passing arrays as arguments to a function, the function call may include the
  531. "array" hint and may be split into multiple lines to improve readability. In
  532. such cases, the normal guidelines for writing arrays still apply:
  533. </para>
  534. <programlisting language="php"><![CDATA[
  535. threeArguments(array(1, 2, 3), 2, 3);
  536. threeArguments(array(1, 2, 3, 'Zend', 'Studio',
  537. $a, $b, $c,
  538. 56.44, $d, 500), 2, 3);
  539. ]]></programlisting>
  540. </sect3>
  541. </sect2>
  542. <sect2 id="coding-standard.coding-style.control-statements">
  543. <title>Control Statements</title>
  544. <sect3 id="coding-standard.coding-style.control-statements.if-else-elseif">
  545. <title>If/Else/Elseif</title>
  546. <para>
  547. Control statements based on the <code>if</code> and <code>elseif</code>
  548. constructs must have a single space before the opening parenthesis of the
  549. conditional and a single space after the closing parenthesis.
  550. </para>
  551. <para>
  552. Within the conditional statements between the parentheses, operators must be
  553. separated by spaces for readability. Inner parentheses are encouraged to improve
  554. logical grouping for larger conditional expressions.
  555. </para>
  556. <para>
  557. The opening brace is written on the same line as the conditional statement. The
  558. closing brace is always written on its own line. Any content within the braces
  559. must be indented using four spaces.
  560. </para>
  561. <programlisting language="php"><![CDATA[
  562. if ($a != 2) {
  563. $a = 2;
  564. }
  565. ]]></programlisting>
  566. <para>
  567. For "if" statements that include "elseif" or "else", the formatting conventions
  568. are similar to the "if" construct. The following examples demonstrate proper
  569. formatting for "if" statements with "else" and/or "elseif" constructs:
  570. </para>
  571. <programlisting language="php"><![CDATA[
  572. if ($a != 2) {
  573. $a = 2;
  574. } else {
  575. $a = 7;
  576. }
  577. if ($a != 2) {
  578. $a = 2;
  579. } elseif ($a == 3) {
  580. $a = 4;
  581. } else {
  582. $a = 7;
  583. }
  584. ]]></programlisting>
  585. <para>
  586. <acronym>PHP</acronym> allows statements to be written without braces in some
  587. circumstances. This coding standard makes no differentiation- all "if",
  588. "elseif" or "else" statements must use braces.
  589. </para>
  590. <para>
  591. Use of the "elseif" construct is permitted but strongly discouraged in favor of
  592. the "else if" combination.
  593. </para>
  594. </sect3>
  595. <sect3 id="coding-standards.coding-style.control-statements.switch">
  596. <title>Switch</title>
  597. <para>
  598. Control statements written with the "switch" statement must have a single space
  599. before the opening parenthesis of the conditional statement and after the
  600. closing parenthesis.
  601. </para>
  602. <para>
  603. All content within the "switch" statement must be indented using four spaces.
  604. Content under each "case" statement must be indented using an additional four
  605. spaces.
  606. </para>
  607. <programlisting language="php"><![CDATA[
  608. switch ($numPeople) {
  609. case 1:
  610. break;
  611. case 2:
  612. break;
  613. default:
  614. break;
  615. }
  616. ]]></programlisting>
  617. <para>
  618. The construct <code>default</code> should never be omitted from a
  619. <code>switch</code> statement.
  620. </para>
  621. <note>
  622. <para>
  623. <emphasis>Note</emphasis>: It is sometimes useful to write a
  624. <code>case</code> statement which falls through to the next case by not
  625. including a <code>break</code> or <code>return</code> within that case. To
  626. distinguish these cases from bugs, any <code>case</code> statement where
  627. <code>break</code> or <code>return</code> are omitted should contain a
  628. comment indicating that the break was intentionally omitted.
  629. </para>
  630. </note>
  631. </sect3>
  632. </sect2>
  633. <sect2 id="coding-standards.inline-documentation">
  634. <title>Inline Documentation</title>
  635. <sect3 id="coding-standards.inline-documentation.documentation-format">
  636. <title>Documentation Format</title>
  637. <para>
  638. All documentation blocks ("docblocks") must be compatible with the phpDocumentor
  639. format. Describing the phpDocumentor format is beyond the scope of this
  640. document. For more information, visit: <ulink
  641. url="http://phpdoc.org/">http://phpdoc.org/</ulink>
  642. </para>
  643. <para>
  644. All class files must contain a "file-level" docblock at the top of each file and
  645. a "class-level" docblock immediately above each class. Examples of such
  646. docblocks can be found below.
  647. </para>
  648. </sect3>
  649. <sect3 id="coding-standards.inline-documentation.files">
  650. <title>Files</title>
  651. <para>
  652. Every file that contains <acronym>PHP</acronym> code must have a docblock at
  653. the top of the file that contains these phpDocumentor tags at a minimum:
  654. </para>
  655. <programlisting language="php"><![CDATA[
  656. /**
  657. * Short description for file
  658. *
  659. * Long description for file (if any)...
  660. *
  661. * LICENSE: Some license information
  662. *
  663. * @copyright 2008 Zend Technologies
  664. * @license http://framework.zend.com/license BSD License
  665. * @version $Id:$
  666. * @link http://framework.zend.com/package/PackageName
  667. * @since File available since Release 1.5.0
  668. */
  669. ]]></programlisting>
  670. </sect3>
  671. <sect3 id="coding-standards.inline-documentation.classes">
  672. <title>Classes</title>
  673. <para>
  674. Every class must have a docblock that contains these phpDocumentor tags at a
  675. minimum:
  676. </para>
  677. <programlisting language="php"><![CDATA[
  678. /**
  679. * Short description for class
  680. *
  681. * Long description for class (if any)...
  682. *
  683. * @copyright 2008 Zend Technologies
  684. * @license http://framework.zend.com/license BSD License
  685. * @version Release: @package_version@
  686. * @link http://framework.zend.com/package/PackageName
  687. * @since Class available since Release 1.5.0
  688. * @deprecated Class deprecated in Release 2.0.0
  689. */
  690. ]]></programlisting>
  691. </sect3>
  692. <sect3 id="coding-standards.inline-documentation.functions">
  693. <title>Functions</title>
  694. <para>
  695. Every function, including object methods, must have a docblock that contains at
  696. a minimum:
  697. </para>
  698. <itemizedlist>
  699. <listitem><para>A description of the function</para></listitem>
  700. <listitem><para>All of the arguments</para></listitem>
  701. <listitem><para>All of the possible return values</para></listitem>
  702. </itemizedlist>
  703. <para>
  704. It is not necessary to use the "@access" tag because the access level is already
  705. known from the "public", "private", or "protected" modifier used to declare the
  706. function.
  707. </para>
  708. <para>
  709. If a function or method may throw an exception, use @throws for all known
  710. exception classes:
  711. </para>
  712. <programlisting language="php"><![CDATA[
  713. @throws exceptionclass [description]
  714. ]]></programlisting>
  715. </sect3>
  716. </sect2>
  717. </sect1>
  718. </appendix>
  719. <!--
  720. vim:se ts=4 sw=4 et:
  721. -->