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