coding_standard.xml 45 KB

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