CODING_STANDARDS 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. ========================
  2. PHP Coding Standards
  3. ========================
  4. This file lists several standards that any programmer adding or changing
  5. code in PHP should follow. Since this file was added at a very late
  6. stage of the development of PHP v3.0, the code base does not fully
  7. follow it, but new features are going in that general direction. Many
  8. sections have been recoded to use these rules.
  9. Code Implementation
  10. -------------------
  11. 0. Document your code in source files and the manual. [tm]
  12. 1. Functions that are given pointers to resources should not free them
  13. For instance, ``function int mail(char *to, char *from)`` should NOT free
  14. to and/or from.
  15. Exceptions:
  16. - The function's designated behavior is freeing that resource. E.g. efree()
  17. - The function is given a boolean argument, that controls whether or not
  18. the function may free its arguments (if true - the function must free its
  19. arguments, if false - it must not)
  20. - Low-level parser routines, that are tightly integrated with the token
  21. cache and the bison code for minimum memory copying overhead.
  22. 2. Functions that are tightly integrated with other functions within the
  23. same module, and rely on each other non-trivial behavior, should be
  24. documented as such and declared 'static'. They should be avoided if
  25. possible.
  26. 3. Use definitions and macros whenever possible, so that constants have
  27. meaningful names and can be easily manipulated. The only exceptions
  28. to this rule are 0 and 1, when used as false and true (respectively).
  29. Any other use of a numeric constant to specify different behavior
  30. or actions should be done through a #define.
  31. 4. When writing functions that deal with strings, be sure to remember
  32. that PHP holds the length property of each string, and that it
  33. shouldn't be calculated with strlen(). Write your functions in such
  34. a way so that they'll take advantage of the length property, both
  35. for efficiency and in order for them to be binary-safe.
  36. Functions that change strings and obtain their new lengths while
  37. doing so, should return that new length, so it doesn't have to be
  38. recalculated with strlen() (e.g. php_addslashes())
  39. 5. NEVER USE strncat(). If you're absolutely sure you know what you're doing,
  40. check its man page again, and only then, consider using it, and even then,
  41. try avoiding it.
  42. 6. Use ``PHP_*`` macros in the PHP source, and ``ZEND_*`` macros in the Zend
  43. part of the source. Although the ``PHP_*`` macro's are mostly aliased to the
  44. ``ZEND_*`` macros it gives a better understanding on what kind of macro
  45. you're calling.
  46. 7. When commenting out code using a #if statement, do NOT use 0 only. Instead
  47. use "<git username here>_0". For example, #if FOO_0, where FOO is your
  48. git user foo. This allows easier tracking of why code was commented out,
  49. especially in bundled libraries.
  50. 8. Do not define functions that are not available. For instance, if a
  51. library is missing a function, do not define the PHP version of the
  52. function, and do not raise a run-time error about the function not
  53. existing. End users should use function_exists() to test for the
  54. existence of a function
  55. 9. Prefer emalloc(), efree(), estrdup(), etc. to their standard C library
  56. counterparts. These functions implement an internal "safety-net"
  57. mechanism that ensures the deallocation of any unfreed memory at the
  58. end of a request. They also provide useful allocation and overflow
  59. information while running in debug mode.
  60. In almost all cases, memory returned to the engine must be allocated
  61. using emalloc().
  62. The use of malloc() should be limited to cases where a third-party
  63. library may need to control or free the memory, or when the memory in
  64. question needs to survive between multiple requests.
  65. User Functions/Methods Naming Conventions
  66. ------------------
  67. 1. Function names for user-level functions should be enclosed with in
  68. the PHP_FUNCTION() macro. They should be in lowercase, with words
  69. underscore delimited, with care taken to minimize the letter count.
  70. Abbreviations should not be used when they greatly decrease the
  71. readability of the function name itself::
  72. Good:
  73. 'str_word_count'
  74. 'array_key_exists'
  75. Ok:
  76. 'date_interval_create_from_date_string'
  77. (could be 'date_intvl_create_from_date_str'?)
  78. 'get_html_translation_table'
  79. (could be 'html_get_trans_table'?)
  80. Bad:
  81. 'hw_GetObjectByQueryCollObj'
  82. 'pg_setclientencoding'
  83. 'jf_n_s_i'
  84. 2. If they are part of a "parent set" of functions, that parent should
  85. be included in the user function name, and should be clearly related
  86. to the parent program or function family. This should be in the form
  87. of ``parent_*``::
  88. A family of 'foo' functions, for example:
  89. Good:
  90. 'foo_select_bar'
  91. 'foo_insert_baz'
  92. 'foo_delete_baz'
  93. Bad:
  94. 'fooselect_bar'
  95. 'fooinsertbaz'
  96. 'delete_foo_baz'
  97. 3. Function names used by user functions should be prefixed
  98. with ``_php_``, and followed by a word or an underscore-delimited list of
  99. words, in lowercase letters, that describes the function. If applicable,
  100. they should be declared 'static'.
  101. 4. Variable names must be meaningful. One letter variable names must be
  102. avoided, except for places where the variable has no real meaning or
  103. a trivial meaning (e.g. for (i=0; i<100; i++) ...).
  104. 5. Variable names should be in lowercase. Use underscores to separate
  105. between words.
  106. 6. Method names follow the 'studlyCaps' (also referred to as 'bumpy case'
  107. or 'camel caps') naming convention, with care taken to minimize the
  108. letter count. The initial letter of the name is lowercase, and each
  109. letter that starts a new 'word' is capitalized::
  110. Good:
  111. 'connect()'
  112. 'getData()'
  113. 'buildSomeWidget()'
  114. Bad:
  115. 'get_Data()'
  116. 'buildsomewidget'
  117. 'getI()'
  118. 7. Class names should be descriptive nouns in PascalCase and as short as
  119. possible. Each word in the class name should start with a capital letter,
  120. without underscore delimiters. The class name should be prefixed with the
  121. name of the "parent set" (e.g. the name of the extension) if no namespaces
  122. are used. Abbreviations and acronyms as well as initialisms should be
  123. avoided wherever possible, unless they are much more widely used than the
  124. long form (e.g. HTTP or URL). Abbreviations start with a capital letter
  125. followed by lowercase letters, whereas acronyms and initialisms are written
  126. according to their standard notation. Usage of acronyms and initialisms is
  127. not allowed if they are not widely adopted and recognized as such.
  128. Good:
  129. 'Curl'
  130. 'CurlResponse'
  131. 'HTTPStatusCode'
  132. 'URL'
  133. 'BTreeMap' (B-tree Map)
  134. 'Id' (Identifier)
  135. 'ID' (Identity Document)
  136. 'Char' (Character)
  137. 'Intl' (Internationalization)
  138. 'Radar' (Radio Detecting and Ranging)
  139. Bad:
  140. 'curl'
  141. 'curl_response'
  142. 'HttpStatusCode'
  143. 'Url'
  144. 'BtreeMap'
  145. 'ID' (Identifier)
  146. 'CHAR'
  147. 'INTL'
  148. 'RADAR' (Radio Detecting and Ranging)
  149. Internal Function Naming Conventions
  150. ----------------------
  151. 1. Functions that are part of the external API should be named
  152. 'php_modulename_function()' to avoid symbol collision. They should be in
  153. lowercase, with words underscore delimited. Exposed API must be defined
  154. in 'php_modulename.h'.
  155. PHPAPI char *php_session_create_id(PS_CREATE_SID_ARGS);
  156. Unexposed module function should be static and should not be defined in
  157. 'php_modulename.h'.
  158. static int php_session_destroy()
  159. 2. Main module source file must be named 'modulename.c'.
  160. 3. Header file that is used by other sources must be named 'php_modulename.h'.
  161. Syntax and indentation
  162. ----------------------
  163. 1. Never use C++ style comments (i.e. // comment). Always use C-style
  164. comments instead. PHP is written in C, and is aimed at compiling
  165. under any ANSI-C compliant compiler. Even though many compilers
  166. accept C++-style comments in C code, you have to ensure that your
  167. code would compile with other compilers as well.
  168. The only exception to this rule is code that is Win32-specific,
  169. because the Win32 port is MS-Visual C++ specific, and this compiler
  170. is known to accept C++-style comments in C code.
  171. 2. Use K&R-style. Of course, we can't and don't want to
  172. force anybody to use a style he or she is not used to, but,
  173. at the very least, when you write code that goes into the core
  174. of PHP or one of its standard modules, please maintain the K&R
  175. style. This applies to just about everything, starting with
  176. indentation and comment styles and up to function declaration
  177. syntax. Also see Indentstyle.
  178. Indentstyle: http://www.catb.org/~esr/jargon/html/I/indent-style.html
  179. 3. Be generous with whitespace and braces. Keep one empty line between the
  180. variable declaration section and the statements in a block, as well as
  181. between logical statement groups in a block. Maintain at least one empty
  182. line between two functions, preferably two. Always prefer::
  183. if (foo) {
  184. bar;
  185. }
  186. to:
  187. if(foo)bar;
  188. 4. When indenting, use the tab character. A tab is expected to represent
  189. four spaces. It is important to maintain consistency in indenture so
  190. that definitions, comments, and control structures line up correctly.
  191. 5. Preprocessor statements (#if and such) MUST start at column one. To
  192. indent preprocessor directives you should put the # at the beginning
  193. of a line, followed by any number of whitespace.
  194. Testing
  195. -------
  196. 1. Extensions should be well tested using *.phpt tests. Read about that
  197. in README.TESTING.
  198. Documentation and Folding Hooks
  199. -------------------------------
  200. In order to make sure that the online documentation stays in line with
  201. the code, each user-level function should have its user-level function
  202. prototype before it along with a brief one-line description of what the
  203. function does. It would look like this::
  204. /* {{{ proto int abs(int number)
  205. Returns the absolute value of the number */
  206. PHP_FUNCTION(abs)
  207. {
  208. ...
  209. }
  210. /* }}} */
  211. The {{{ symbols are the default folding symbols for the folding mode in
  212. Emacs and vim (set fdm=marker). Folding is very useful when dealing with
  213. large files because you can scroll through the file quickly and just unfold
  214. the function you wish to work on. The }}} at the end of each function marks
  215. the end of the fold, and should be on a separate line.
  216. The "proto" keyword there is just a helper for the doc/genfuncsummary script
  217. which generates a full function summary. Having this keyword in front of the
  218. function prototypes allows us to put folds elsewhere in the code without
  219. messing up the function summary.
  220. Optional arguments are written like this::
  221. /* {{{ proto object imap_header(int stream_id, int msg_no [, int from_length [, int subject_length [, string default_host]]])
  222. Returns a header object with the defined parameters */
  223. And yes, please keep the prototype on a single line, even if that line
  224. is massive.
  225. New and Experimental Functions
  226. -----------------------------------
  227. To reduce the problems normally associated with the first public
  228. implementation of a new set of functions, it has been suggested
  229. that the first implementation include a file labeled 'EXPERIMENTAL'
  230. in the function directory, and that the functions follow the
  231. standard prefixing conventions during their initial implementation.
  232. The file labelled 'EXPERIMENTAL' should include the following
  233. information::
  234. Any authoring information (known bugs, future directions of the module).
  235. Ongoing status notes which may not be appropriate for Git comments.
  236. In general new features should go to PECL or experimental branches until
  237. there are specific reasons for directly adding it to the core distribution.
  238. Aliases & Legacy Documentation
  239. -----------------------------------
  240. You may also have some deprecated aliases with close to duplicate
  241. names, for example, somedb_select_result and somedb_selectresult. For
  242. documentation purposes, these will only be documented by the most
  243. current name, with the aliases listed in the documentation for
  244. the parent function. For ease of reference, user-functions with
  245. completely different names, that alias to the same function (such as
  246. highlight_file and show_source), will be separately documented. The
  247. proto should still be included, describing which function is aliased.
  248. Backwards compatible functions and names should be maintained as long
  249. as the code can be reasonably be kept as part of the codebase. See the
  250. README in the PHP documentation repository for more information on
  251. documentation.