@@ -9,7 +9,7 @@ rewritten to comply with these rules.
9
9
10
10
1 . Document your code in source files and the manual. (tm)
11
11
12
- 2 . Functions that are given pointers to resources should not free them.
12
+ 1 . Functions that are given pointers to resources should not free them.
13
13
14
14
For instance, ` function int mail(char *to, char *from) ` should NOT free ` to `
15
15
and/or ` from ` .
@@ -26,42 +26,42 @@ rewritten to comply with these rules.
26
26
* Low-level parser routines, that are tightly integrated with the token
27
27
cache and the bison code for minimum memory copying overhead.
28
28
29
- 3 . Functions that are tightly integrated with other functions within the same
29
+ 1 . Functions that are tightly integrated with other functions within the same
30
30
module, and rely on each other's non-trivial behavior, should be documented as
31
31
such and declared ` static ` . They should be avoided if possible.
32
32
33
- 4 . Use definitions and macros whenever possible, so that constants have
33
+ 1 . Use definitions and macros whenever possible, so that constants have
34
34
meaningful names and can be easily manipulated. Any use of a numeric
35
35
constant to specify different behavior or actions should be done through
36
36
a ` #define ` .
37
37
38
- 5 . When writing functions that deal with strings, be sure to remember that PHP
38
+ 1 . When writing functions that deal with strings, be sure to remember that PHP
39
39
holds the length property of each string, and that it shouldn't be
40
40
calculated with ` strlen() ` . Write your functions in such a way so that
41
41
they'll take advantage of the length property, both for efficiency and in
42
42
order for them to be binary-safe. Functions that change strings and obtain
43
43
their new lengths while doing so, should return that new length, so it
44
44
doesn't have to be recalculated with ` strlen() ` (e.g. ` php_addslashes() ` ).
45
45
46
- 6 . NEVER USE ` strncat() ` . If you're absolutely sure you know what you're doing,
46
+ 1 . NEVER USE ` strncat() ` . If you're absolutely sure you know what you're doing,
47
47
check its man page again, and only then, consider using it, and even then,
48
48
try avoiding it.
49
49
50
- 7 . Use ` PHP_* ` macros in the PHP source, and ` ZEND_* ` macros in the Zend part of
50
+ 1 . Use ` PHP_* ` macros in the PHP source, and ` ZEND_* ` macros in the Zend part of
51
51
the source. Although the ` PHP_* ` macros are mostly aliased to the ` ZEND_* `
52
52
macros it gives a better understanding on what kind of macro you're calling.
53
53
54
- 8 . When commenting out code using a ` #if ` statement, do NOT use ` 0 ` only.
55
- Instead use ` "<git username here>_0" ` . For example, ` #if FOO_0 ` , where ` FOO `
56
- is your git user ` foo ` . This allows easier tracking of why code was
57
- commented out, especially in bundled libraries.
54
+ 1 . When commenting out code using a ` #if ` statement, do NOT use ` 0 ` only.
55
+ Instead, use ` "<git username here>_0" ` . For example, ` #if FOO_0 ` ,
56
+ where ` FOO ` is your git user ` foo ` . This allows easier tracking of why
57
+ code was commented out, especially in bundled libraries.
58
58
59
- 9 . Do not define functions that are not available. For instance, if a library is
59
+ 1 . Do not define functions that are not available. For instance, if a library is
60
60
missing a function, do not define the PHP version of the function, and do
61
61
not raise a run-time error about the function not existing. End users should
62
62
use ` function_exists() ` to test for the existence of a function.
63
63
64
- 10 . Prefer ` emalloc() ` , ` efree() ` , ` estrdup() ` , etc. to their standard C library
64
+ 1 . Prefer ` emalloc() ` , ` efree() ` , ` estrdup() ` , etc. to their standard C library
65
65
counterparts. These functions implement an internal "safety-net" mechanism
66
66
that ensures the deallocation of any unfreed memory at the end of a request.
67
67
They also provide useful allocation and overflow information while running
@@ -106,7 +106,7 @@ rewritten to comply with these rules.
106
106
jf_n_s_i
107
107
```
108
108
109
- 2 . If they are part of a "parent set" of functions, that parent should be
109
+ 1 . If they are part of a "parent set" of functions, that parent should be
110
110
included in the user function name, and should be clearly related to the
111
111
parent program or function family. This should be in the form of `parent_*`:
112
112
@@ -128,19 +128,19 @@ rewritten to comply with these rules.
128
128
delete_foo_baz
129
129
```
130
130
131
- 3 . Function names used by user functions should be prefixed with `_php_`, and
131
+ 1 . Function names used by user functions should be prefixed with `_php_`, and
132
132
followed by a word or an underscore-delimited list of words, in lowercase
133
133
letters, that describes the function. If applicable, they should be declared
134
134
`static`.
135
135
136
- 4 . Variable names must be meaningful. One letter variable names must be avoided,
136
+ 1 . Variable names must be meaningful. One letter variable names must be avoided,
137
137
except for places where the variable has no real meaning or a trivial
138
138
meaning (e.g. `for (i=0; i<100 ; i++) ...`).
139
139
140
- 5 . Variable names should be in lowercase. Use underscores to separate between
140
+ 1 . Variable names should be in lowercase. Use underscores to separate between
141
141
words.
142
142
143
- 6 . Method names follow the *studlyCaps* (also referred to as *bumpy case* or
143
+ 1 . Method names follow the *studlyCaps* (also referred to as *bumpy case* or
144
144
*camel caps*) naming convention, with care taken to minimize the letter
145
145
count. The initial letter of the name is lowercase, and each letter that
146
146
starts a new `word` is capitalized:
@@ -161,7 +161,7 @@ rewritten to comply with these rules.
161
161
getI()
162
162
```
163
163
164
- 7 . Class names should be descriptive nouns in *PascalCase* and as short as
164
+ 1 . Class names should be descriptive nouns in *PascalCase* and as short as
165
165
possible. Each word in the class name should start with a capital letter,
166
166
without underscore delimiters. The class name should be prefixed with the
167
167
name of the " parent set" (e.g. the name of the extension) if no namespaces
@@ -219,28 +219,20 @@ rewritten to comply with these rules.
219
219
static int php_session_destroy()
220
220
```
221
221
222
- 2 . Main module source file must be named `modulename.c`.
222
+ 1 . Main module source file must be named `modulename.c`.
223
223
224
- 3 . Header file that is used by other sources must be named `php_modulename.h`.
224
+ 1 . Header file that is used by other sources must be named `php_modulename.h`.
225
225
226
226
## Syntax and indentation
227
227
228
- 1. Never use C++ style comments (i.e. ` //` comment). Always use C-style comments
229
- instead. PHP is written in C, and is aimed at compiling under any ANSI-C
230
- compliant compiler. Even though many compilers accept C++-style comments in
231
- C code, you have to ensure that your code would compile with other compilers
232
- as well. The only exception to this rule is code that is Win32-specific,
233
- because the Win32 port is MS-Visual C++ specific, and this compiler is known
234
- to accept C++-style comments in C code.
235
-
236
- 2. Use K&R-style. Of course, we can ' t and don' t want to force anybody to use a
228
+ 1. Use K&R-style. Of course, we can ' t and don' t want to force anybody to use a
237
229
style he or she is not used to, but, at the very least, when you write code
238
230
that goes into the core of PHP or one of its standard modules, please
239
231
maintain the K&R style. This applies to just about everything, starting with
240
232
indentation and comment styles and up to function declaration syntax. Also
241
233
see [Indentstyle](http: //www.catb.org /~esr /jargon /html /I /indent-style.html).
242
234
243
- 3 . Be generous with whitespace and braces. Keep one empty line between the
235
+ 1 . Be generous with whitespace and braces. Keep one empty line between the
244
236
variable declaration section and the statements in a block, as well as
245
237
between logical statement groups in a block. Maintain at least one empty
246
238
line between two functions, preferably two. Always prefer:
@@ -257,14 +249,20 @@ rewritten to comply with these rules.
257
249
if(foo)bar;
258
250
```
259
251
260
- 4 . When indenting, use the tab character. A tab is expected to represent four
252
+ 1 . When indenting, use the tab character. A tab is expected to represent four
261
253
spaces. It is important to maintain consistency in indentation so that
262
254
definitions, comments, and control structures line up correctly.
263
255
264
- 5 . Preprocessor statements (`#if` and such) MUST start at column one. To indent
256
+ 1 . Preprocessor statements (`#if` and such) MUST start at column one. To indent
265
257
preprocessor directives you should put the `#` at the beginning of a line,
266
258
followed by any number of spaces.
267
259
260
+ 1. The length of constant string literals should be calculated via ``strlen()``
261
+ instead of using ``sizeof()-1`` as it is clearer and any modern compiler
262
+ will optimize it away. Legacy usages of the latter style exists within the
263
+ codebase but should not be refactored, unless larger refactoring around that
264
+ code is taking place.
265
+
268
266
## Testing
269
267
270
268
1. Extensions should be well tested using `*.phpt` tests. Read more at
0 commit comments