Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 270 lines (207 sloc) 11.519 kB
aceaabc @zsuraski PHP 4.0
zsuraski authored
1 PHP Coding Standards
2 ====================
3
4
5 This file lists several standards that any programmer, adding or changing
6 code in PHP, should follow. Since this file was added at a very late
7 stage of the development of PHP v3.0, the code base does not (yet) fully
45807e6 @jparise Added a brief section documenting the preference of tabs over spaces.
jparise authored
8 follow it, but it's going in that general direction. Since we are now
9 well into the version 4 releases, many sections have been recoded to use
10 these rules.
aceaabc @zsuraski PHP 4.0
zsuraski authored
11
12
13 Code Implementation
14 -------------------
15
6d10159 comments & php_error_docref
Marcus Boerger authored
16 [0] Document your code in source files and the manual. [tm]
17
aceaabc @zsuraski PHP 4.0
zsuraski authored
18 [1] Functions that are given pointers to resources should not free them
19
20 For instance, function int mail(char *to, char *from) should NOT free
21 to and/or from.
22 Exceptions:
23
24 - The function's designated behavior is freeing that resource. E.g. efree()
25 - The function is given a boolean argument, that controls whether or not
26 the function may free its arguments (if true - the function must free its
27 arguments, if false - it must not)
28 - Low-level parser routines, that are tightly integrated with the token
29 cache and the bison code for minimum memory copying overhead.
30
31 [2] Functions that are tightly integrated with other functions within the
32 same module, and rely on each other non-trivial behavior, should be
33 documented as such and declared 'static'. They should be avoided if
34 possible.
35
36 [3] Use definitions and macros whenever possible, so that constants have
37 meaningful names and can be easily manipulated. The only exceptions
38 to this rule are 0 and 1, when used as false and true (respectively).
39 Any other use of a numeric constant to specify different behavior
40 or actions should be done through a #define.
41
42 [4] When writing functions that deal with strings, be sure to remember
43 that PHP holds the length property of each string, and that it
44 shouldn't be calculated with strlen(). Write your functions in a such
45 a way so that they'll take advantage of the length property, both
46 for efficiency and in order for them to be binary-safe.
47 Functions that change strings and obtain their new lengths while
48 doing so, should return that new length, so it doesn't have to be
585e338 php4ize a bit.
Sterling Hughes authored
49 recalculated with strlen() (e.g. php_addslashes())
aceaabc @zsuraski PHP 4.0
zsuraski authored
50
6d10159 comments & php_error_docref
Marcus Boerger authored
51 [5] Use php_error_docref() group of functions to report any errors/warnings
52 during code execution. Use descriptive error messages, and try to avoid
53 using identical error strings for different stages of an error. For
54 example, if in order to obtain a URL you have to parse the URL, connect,
55 and retreive the text, assuming something can go wrong at each of these
56 stages, don't report an error "Unable to get URL" on all of them, but
57 instead, write something like "Unable to parse URL", "Unable to connect
58 to URL server" and "Unable to fetch URL text", respectively.
aceaabc @zsuraski PHP 4.0
zsuraski authored
59
652baa7 - Add note how php_error() messages SHOULD (rfc2119) look like.
Markus Fischer authored
60 It has been silently agreed to prefix every php_error() message with the
61 name of the current function if applicable:
62
63 php_error(E_WHATEVER, "%s(): Desc.", get_active_function_name(TSRMLS_C));
64
6d10159 comments & php_error_docref
Marcus Boerger authored
65 This can be done automatically using php_error_docref(). The first
66 parameter, docref, is either NULL or the URL of a page describing the
67 error in detail. If you pass NULL, the reference is generated from
68 the name of the function being executed:
69
70 php_error_docref(NULL TSRMLS_CC, E_WHATEVER, "Desc.");
71
72 If you pass a URL, it can either be a full URL beginning with "http://":
73
74 php_error_docref("http://www.php.net/manual/en/function.fopen#error"
75 TSRMLS_CC, E_WHATEVER, "Desc.");
76
77 Or the name of a manual page without file extension, but with an optional
78 target anchor. Or simply the anchor within the manual page of the current
79 function. When using function names you must replace '_' by '-':
80
81 php_error_docref("function.ext_func#error" TSRMLS_CC, E_WHATEVER, "Desc.");
82
83 To display one or two important parameters after the function name, use
84 php_error_docref1() or php_error_docref2(). For example, file functions
85 should display the name of the file opened:
86
87 php_error_docref1("function.fopen", filename
88 TSRMLS_CC, E_WHATEVER, "Desc.");
89
90 php_error_docref2("function.fopen", filename, openmode
91 TSRMLS_CC, E_WHATEVER, "Desc.");
92
652baa7 - Add note how php_error() messages SHOULD (rfc2119) look like.
Markus Fischer authored
93 Fixing ("unifying") existing php_error() message is a good thing [tm].
94
aceaabc @zsuraski PHP 4.0
zsuraski authored
95 [6] NEVER USE strncat(). If you're absolutely sure you know what you're doing,
96 check its man page again, and only then, consider using it, and even then,
97 try avoiding it.
98
86dba96 Translate to English.
Sean Bright authored
99 [7] Use ZEND_* macros instead of PHP_* macros. Use of PHP_* macros is not
100 recommended. Since most of the PHP_* macros are ZEND_* macro aliases, using
101 the PHP_* macros makes browsing the source code with a tag search harder.
aceaabc @zsuraski PHP 4.0
zsuraski authored
102
86dba96 Translate to English.
Sean Bright authored
103 [8] Use assert(). assert.h is included in php.h if it is available. Not only
104 does good assertion catch bugs, but it also helps with code readability.
69d6f1e Added README.SUBMITTING_PATCH file. Please fix/add/change.
Yasuo Ohgaki authored
105 - Do not use assert for error handling. Use assert only for the
106 condition that must be always true.
107 - Do not use assignments in assert conditions. If you assign inside an
108 assert condition, you risk an elusive bug that would be very difficult
109 to spot in a debug build, due to the side effect of the assignment.
110 Function calls in assert conditions may also cause this problem, if
111 they modify one of their arguments or global variables.
cbc1cd8 Include/enable assert.h/assert() when it is available
Yasuo Ohgaki authored
112
aceaabc @zsuraski PHP 4.0
zsuraski authored
113 Naming Conventions
114 ------------------
115
5cc6344 Updated to reflect recent discussions on php-dev.
Ron Chmara authored
116 [1] Function names for user-level functions should be enclosed with in
aa20347 Updated naming standards as per 9/12 dev/doc discussion.
Ron Chmara authored
117 the PHP_FUNCTION() macro. They should be in lowercase, with words
118 underscore delimited, with care taken to minimize the letter count.
5cc6344 Updated to reflect recent discussions on php-dev.
Ron Chmara authored
119 Abbreviations should not be used when they greatly decrease the
120 readability of the function name itself.
aa20347 Updated naming standards as per 9/12 dev/doc discussion.
Ron Chmara authored
121
122 Good:
123 'mcrypt_enc_self_test'
124 'mysql_list_fields'
125
126 Ok:
127 'mcrypt_module_get_algo_supported_key_sizes'
128 (could be 'mcrypt_mod_get_algo_sup_key_sizes'?)
129 'get_html_translation_table'
130 (could be 'html_get_trans_table'?)
131
132 Bad:
133 'hw_GetObjectByQueryCollObj'
134 'pg_setclientencoding'
5cc6344 Updated to reflect recent discussions on php-dev.
Ron Chmara authored
135 'jf_n_s_i'
136
137
138 [2] If they are part of a "parent set" of functions, that parent should
139 be included in the user function name, and should be clearly related
140 to the parent program or function family. This should be in the form
141 of parent_*.
142
143 A family of 'foo' functions, for example:
144 Good:
145 'foo_select_bar'
146 'foo_insert_baz'
147 'foo_delete_baz'
aceaabc @zsuraski PHP 4.0
zsuraski authored
148
5cc6344 Updated to reflect recent discussions on php-dev.
Ron Chmara authored
149 Bad:
150 'fooselect_bar'
151 'fooinsertbaz'
152 'delete_foo_baz'
153
154 [3] Function names used by user functions should be prefixed
585e338 php4ize a bit.
Sterling Hughes authored
155 with "_php_", and followed by a word or an underscore-delimited list of
aceaabc @zsuraski PHP 4.0
zsuraski authored
156 words, in lowercase letters, that describes the function. If applicable,
157 they should be declared 'static'.
158
5cc6344 Updated to reflect recent discussions on php-dev.
Ron Chmara authored
159 [4] Variable names must be meaningful. One letter variable names must be
aceaabc @zsuraski PHP 4.0
zsuraski authored
160 avoided, except for places where the variable has no real meaning or
161 a trivial meaning (e.g. for (i=0; i<100; i++) ...).
162
8a5402c Fixed some spelling errors.
Egon Schmid authored
163 [5] Variable names should be in lowercase. Use underscores to separate
aceaabc @zsuraski PHP 4.0
zsuraski authored
164 between words.
165
166
167 Syntax and indentation
168 ----------------------
169
170 [1] Never use C++ style comments (i.e. // comment). Always use C-style
171 comments instead. PHP is written in C, and is aimed at compiling
172 under any ANSI-C compliant compiler. Even though many compilers
173 accept C++-style comments in C code, you have to ensure that your
174 code would compile with other compilers as well.
175 The only exception to this rule is code that is Win32-specific,
176 because the Win32 port is MS-Visual C++ specific, and this compiler
177 is known to accept C++-style comments in C code.
178
179 [2] Use K&R-style. Of course, we can't and don't want to
45807e6 @jparise Added a brief section documenting the preference of tabs over spaces.
jparise authored
180 force anybody to use a style he or she is not used to, but,
aceaabc @zsuraski PHP 4.0
zsuraski authored
181 at the very least, when you write code that goes into the core
182 of PHP or one of its standard modules, please maintain the K&R
183 style. This applies to just about everything, starting with
8a5402c Fixed some spelling errors.
Egon Schmid authored
184 indentation and comment styles and up to function declaration
aceaabc @zsuraski PHP 4.0
zsuraski authored
185 syntax.
7d220af small clarification
Hartmut Holzgraefe authored
186
187 (see also http://www.tuxedo.org/~esr/jargon/html/entry/indent-style.html)
aceaabc @zsuraski PHP 4.0
zsuraski authored
188
45807e6 @jparise Added a brief section documenting the preference of tabs over spaces.
jparise authored
189 [3] Be generous with whitespace and braces. Always prefer:
190
191 if (foo) {
aceaabc @zsuraski PHP 4.0
zsuraski authored
192 bar;
193 }
45807e6 @jparise Added a brief section documenting the preference of tabs over spaces.
jparise authored
194
195 to:
196
aceaabc @zsuraski PHP 4.0
zsuraski authored
197 if(foo)bar;
45807e6 @jparise Added a brief section documenting the preference of tabs over spaces.
jparise authored
198
8a5402c Fixed some spelling errors.
Egon Schmid authored
199 Keep one empty line between the variable declaration section and
aceaabc @zsuraski PHP 4.0
zsuraski authored
200 the statements in a block, as well as between logical statement
201 groups in a block. Maintain at least one empty line between
202 two functions, preferably two.
203
45807e6 @jparise Added a brief section documenting the preference of tabs over spaces.
jparise authored
204 [4] When indenting, use the tab character. A tab is expected to represent
205 four spaces. It is important to maintain consistency in indenture so
206 that definitions, comments, and control structures line up correctly.
207
aceaabc @zsuraski PHP 4.0
zsuraski authored
208 Documentation and Folding Hooks
209 -------------------------------
210
211 In order to make sure that the online documentation stays in line with
212 the code, each user-level function should have its user-level function
213 prototype before it along with a brief one-line description of what the
214 function does. It would look like this:
215
216 /* {{{ proto int abs(int number)
8a5402c Fixed some spelling errors.
Egon Schmid authored
217 Returns the absolute value of the number */
585e338 php4ize a bit.
Sterling Hughes authored
218 PHP_FUNCTION(abs)
219 {
aceaabc @zsuraski PHP 4.0
zsuraski authored
220 ...
221 }
222 /* }}} */
223
224 The {{{ symbols are the default folding symbols for the folding mode in
25c3a3a @rlerdorf vim-6 does folding - clean up a bunch of missing folding tags plus
rlerdorf authored
225 Emacs and vim (set fdm=marker). Folding is very useful when dealing with
226 large files because you can scroll through the file quickly and just unfold
227 the function you wish to work on. The }}} at the end of each function marks
228 the end of the fold, and should be on a separate line.
aceaabc @zsuraski PHP 4.0
zsuraski authored
229
230 The "proto" keyword there is just a helper for the doc/genfuncsummary script
231 which generates a full function summary. Having this keyword in front of the
232 function prototypes allows us to put folds elsewhere in the code without
233 messing up the function summary.
234
235 Optional arguments are written like this:
236
237 /* {{{ proto object imap_header(int stream_id, int msg_no [, int from_length [, int subject_length [, string default_host]]])
8a5402c Fixed some spelling errors.
Egon Schmid authored
238 Returns a header object with the defined parameters */
aceaabc @zsuraski PHP 4.0
zsuraski authored
239
5cc6344 Updated to reflect recent discussions on php-dev.
Ron Chmara authored
240 And yes, please keep the prototype on a single line, even if that line
241 is massive.
242
243 New and Experimental Functions
244 -----------------------------------
245 To reduce the problems normally associated with the first public
246 implementation of a new set of functions, it has been suggested
247 that the first implementation include a file labeled 'EXPERIMENTAL'
248 in the function directory, and that the functions follow the
249 standard prefixing conventions during their initial implementation.
250
251 The file labelled 'EXPERIMENTAL' should include the following
252 information:
8a5402c Fixed some spelling errors.
Egon Schmid authored
253 Any authoring information (known bugs, future directions of the module).
5cc6344 Updated to reflect recent discussions on php-dev.
Ron Chmara authored
254 Ongoing status notes which may not be appropriate for CVS comments.
aceaabc @zsuraski PHP 4.0
zsuraski authored
255
d47e483 As per andi, similar names are for legacy reasons only.
Ron Chmara authored
256 Aliases & Legacy Documentation
257 -----------------------------------
258 You may also have some deprecated aliases with close to duplicate
aa20347 Updated naming standards as per 9/12 dev/doc discussion.
Ron Chmara authored
259 names, for example, somedb_select_result and somedb_selectresult. For
8a5402c Fixed some spelling errors.
Egon Schmid authored
260 documentation purposes, these will only be documented by the most
d47e483 As per andi, similar names are for legacy reasons only.
Ron Chmara authored
261 current name, with the aliases listed in the documentation for
aa20347 Updated naming standards as per 9/12 dev/doc discussion.
Ron Chmara authored
262 the parent function. For ease of reference, user-functions with
263 completely different names, that alias to the same function (such as
264 highlight_file and show_source), will be separately documented. The
265 proto should still be included, describing which function is aliased.
266
d47e483 As per andi, similar names are for legacy reasons only.
Ron Chmara authored
267 Backwards compatible functions and names should be maintained as long
268 as the code can be reasonably be kept as part of the codebase. See
8a5402c Fixed some spelling errors.
Egon Schmid authored
269 /phpdoc/README for more information on documentation.
Something went wrong with that request. Please try again.