You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
6.2 KiB

27 years ago
  1. PHP Coding Standards
  2. ====================
  3. This file lists several standards that any programmer, adding or changing
  4. code in PHP, should follow. Since this file was added at a very late
  5. stage of the development of PHP v3.0, the code base does not (yet) fully
  6. follow it, but it's going in that general direction.
  7. This is an initial version - it'll most probably grow as time passes.
  8. Code Implementation
  9. -------------------
  10. [1] Functions that are given pointers to resources should not free them
  11. For instance, function int mail(char *to, char *from) should NOT free
  12. to and/or from.
  13. Exceptions:
  14. - The function's designated behavior is freeing that resource. E.g. efree()
  15. - The function is given a boolean argument, that controls whether or not
  16. the function may free its arguments (if true - the function must free its
  17. arguments, if false - it must not)
  18. - Low-level parser routines, that are tightly integrated with the token
  19. cache and the bison code for minimum memory copying overhead.
  20. [2] Functions that are tightly integrated with other functions within the
  21. same module, and rely on each other non-trivial behavior, should be
  22. documented as such and declared 'static'. They should be avoided if
  23. possible.
  24. [3] Use definitions and macros whenever possible, so that constants have
  25. meaningful names and can be easily manipulated. The only exceptions
  26. to this rule are 0 and 1, when used as false and true (respectively).
  27. Any other use of a numeric constant to specify different behavior
  28. or actions should be done through a #define.
  29. [4] When writing functions that deal with strings, be sure to remember
  30. that PHP holds the length property of each string, and that it
  31. shouldn't be calculated with strlen(). Write your functions in a such
  32. a way so that they'll take advantage of the length property, both
  33. for efficiency and in order for them to be binary-safe.
  34. Functions that change strings and obtain their new lengths while
  35. doing so, should return that new length, so it doesn't have to be
  36. recalculated with strlen() (e.g. _php3_addslashes())
  37. [5] Use php3_error() to report any errors/warnings during code execution.
  38. Use descriptive error messages, and try to avoid using identical
  39. error strings for different stages of an error. For example,
  40. if in order to obtain a URL you have to parse the URL, connect,
  41. and retreive the text, assuming something can go wrong at each
  42. of these stages, don't report an error "Unable to get URL"
  43. on all of them, but instead, write something like "Unable
  44. to parse URL", "Unable to connect to URL server" and "Unable
  45. to fetch URL text", respectively.
  46. [6] NEVER USE strncat(). If you're absolutely sure you know what you're doing,
  47. check its man page again, and only then, consider using it, and even then,
  48. try avoiding it.
  49. Naming Conventions
  50. ------------------
  51. [1] Function names for user functions implementation should be prefixed with
  52. "php3_", and followed by a word or an underscore-delimited list of words,
  53. in lowercase letters, that describes the function.
  54. [2] Function names used by user functions implementations should be prefixed
  55. with "_php3_", and followed by a word or an underscore-delimited list of
  56. words, in lowercase letters, that describes the function. If applicable,
  57. they should be declared 'static'.
  58. [3] Variable names must be meaningful. One letter variable names must be
  59. avoided, except for places where the variable has no real meaning or
  60. a trivial meaning (e.g. for (i=0; i<100; i++) ...).
  61. [4] Variable names should be in lowercase; Use underscores to seperate
  62. between words.
  63. Syntax and indentation
  64. ----------------------
  65. [1] Never use C++ style comments (i.e. // comment). Always use C-style
  66. comments instead. PHP is written in C, and is aimed at compiling
  67. under any ANSI-C compliant compiler. Even though many compilers
  68. accept C++-style comments in C code, you have to ensure that your
  69. code would compile with other compilers as well.
  70. The only exception to this rule is code that is Win32-specific,
  71. because the Win32 port is MS-Visual C++ specific, and this compiler
  72. is known to accept C++-style comments in C code.
  73. [2] Use K&R-style. Of course, we can't and don't want to
  74. force anybody to use a style she's not used to, but
  75. at the very least, when you write code that goes into the core
  76. of PHP or one of its standard modules, please maintain the K&R
  77. style. This applies to just about everything, starting with
  78. indentation and comment styles and up to function decleration
  79. syntax.
  80. [3] Be generous with whitespace and braces. Always prefer
  81. if (foo) {
  82. bar;
  83. }
  84. to
  85. if(foo)bar;
  86. Keep one empty line between the variable decleration section and
  87. the statements in a block, as well as between logical statement
  88. groups in a block. Maintain at least one empty line between
  89. two functions, preferably two.
  90. Documentation and Folding Hooks
  91. -------------------------------
  92. In order to make sure that the online documentation stays in line with
  93. the code, each user-level function should have its user-level function
  94. prototype before it along with a brief one-line description of what the
  95. function does. It would look like this:
  96. /* {{{ proto int abs(int number)
  97. Return the absolute value of the number */
  98. void php3_abs(INTERNAL_FUNCTION_PARAMETERS) {
  99. ...
  100. }
  101. /* }}} */
  102. The {{{ symbols are the default folding symbols for the folding mode in
  103. Emacs. vim will soon have support for folding as well. Folding is very
  104. useful when dealing with large files because you can scroll through the
  105. file quickly and just unfold the function you wish to work on. The }}}
  106. at the end of each function marks the end of the fold, and should be on
  107. a separate line.
  108. The "proto" keyword there is just a helper for the doc/genfuncsummary script
  109. which generates a full function summary. Having this keyword in front of the
  110. function prototypes allows us to put folds elsewhere in the code without
  111. messing up the function summary.
  112. Optional arguments are written like this:
  113. /* {{{ proto object imap_header(int stream_id, int msg_no [, int from_length [, int subject_length [, string default_host]]])
  114. And yes, please keep everything on a single line, even if that line is massive.