1999-04-08 05:05:13 +08:00
|
|
|
PHP Coding Standards
|
|
|
|
====================
|
|
|
|
|
|
|
|
|
|
|
|
This file lists several standards that any programmer, adding or changing
|
|
|
|
code in PHP, should follow. Since this file was added at a very late
|
|
|
|
stage of the development of PHP v3.0, the code base does not (yet) fully
|
2000-11-27 05:45:44 +08:00
|
|
|
follow it, but it's going in that general direction. Since we are now
|
|
|
|
well into the version 4 releases, many sections have been recoded to use
|
|
|
|
these rules.
|
1999-04-08 05:05:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
Code Implementation
|
|
|
|
-------------------
|
|
|
|
|
2002-08-13 17:42:51 +08:00
|
|
|
[0] Document your code in source files and the manual. [tm]
|
|
|
|
|
1999-04-08 05:05:13 +08:00
|
|
|
[1] Functions that are given pointers to resources should not free them
|
|
|
|
|
|
|
|
For instance, function int mail(char *to, char *from) should NOT free
|
|
|
|
to and/or from.
|
|
|
|
Exceptions:
|
|
|
|
|
|
|
|
- The function's designated behavior is freeing that resource. E.g. efree()
|
|
|
|
- The function is given a boolean argument, that controls whether or not
|
|
|
|
the function may free its arguments (if true - the function must free its
|
|
|
|
arguments, if false - it must not)
|
|
|
|
- Low-level parser routines, that are tightly integrated with the token
|
|
|
|
cache and the bison code for minimum memory copying overhead.
|
|
|
|
|
|
|
|
[2] Functions that are tightly integrated with other functions within the
|
|
|
|
same module, and rely on each other non-trivial behavior, should be
|
|
|
|
documented as such and declared 'static'. They should be avoided if
|
|
|
|
possible.
|
|
|
|
|
|
|
|
[3] Use definitions and macros whenever possible, so that constants have
|
|
|
|
meaningful names and can be easily manipulated. The only exceptions
|
|
|
|
to this rule are 0 and 1, when used as false and true (respectively).
|
|
|
|
Any other use of a numeric constant to specify different behavior
|
|
|
|
or actions should be done through a #define.
|
|
|
|
|
|
|
|
[4] When writing functions that deal with strings, be sure to remember
|
|
|
|
that PHP holds the length property of each string, and that it
|
|
|
|
shouldn't be calculated with strlen(). Write your functions in a such
|
|
|
|
a way so that they'll take advantage of the length property, both
|
|
|
|
for efficiency and in order for them to be binary-safe.
|
|
|
|
Functions that change strings and obtain their new lengths while
|
|
|
|
doing so, should return that new length, so it doesn't have to be
|
2000-09-11 05:07:18 +08:00
|
|
|
recalculated with strlen() (e.g. php_addslashes())
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2003-04-05 01:01:09 +08:00
|
|
|
[5] NEVER USE strncat(). If you're absolutely sure you know what you're doing,
|
1999-04-08 05:05:13 +08:00
|
|
|
check its man page again, and only then, consider using it, and even then,
|
|
|
|
try avoiding it.
|
|
|
|
|
2003-04-05 01:01:09 +08:00
|
|
|
[6] Use PHP_* macros in the PHP source, and ZEND_* macros in the Zend
|
2002-09-09 15:54:11 +08:00
|
|
|
part of the source. Although the PHP_* macro's are mostly aliased to the
|
|
|
|
ZEND_* macros it gives a better understanding on what kind of macro you're
|
|
|
|
calling.
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2003-04-05 01:01:09 +08:00
|
|
|
[7] When commenting out code using a #if statement, do NOT use 0 only. Instead
|
2002-10-30 04:25:09 +08:00
|
|
|
use "<cvs username here>_0". For example, #if FOO_0, where FOO is your
|
2002-08-14 05:44:59 +08:00
|
|
|
cvs user foo. This allows easier tracking of why code was commented out,
|
|
|
|
especially in bundled libraries.
|
|
|
|
|
2003-04-05 01:01:09 +08:00
|
|
|
[8] Do not define functions that are not available. For instance, if a
|
2002-09-09 07:00:31 +08:00
|
|
|
library is missing a function, do not define the PHP version of the
|
|
|
|
function, and do not raise a run-time error about the function not
|
|
|
|
existing. End users should use function_exists() to test for the
|
|
|
|
existence of a function
|
2002-09-09 06:38:57 +08:00
|
|
|
|
2003-04-05 01:01:09 +08:00
|
|
|
[9] Prefer emalloc(), efree(), estrdup(), etc. to their standard C library
|
2002-10-11 01:03:49 +08:00
|
|
|
counterparts. These functions implement an internal "safety-net"
|
|
|
|
mechanism that ensures the deallocation of any unfreed memory at the
|
|
|
|
end of a request. They also provide useful allocation and overflow
|
|
|
|
information while running in debug mode.
|
|
|
|
|
|
|
|
In almost all cases, memory returned to the engine must be allocated
|
|
|
|
using emalloc().
|
|
|
|
|
|
|
|
The use of malloc() should be limited to cases where a third-party
|
|
|
|
library may need to control or free the memory, or when the memory in
|
|
|
|
question needs to survive between multiple requests.
|
|
|
|
|
1999-04-08 05:05:13 +08:00
|
|
|
Naming Conventions
|
|
|
|
------------------
|
|
|
|
|
2000-12-19 14:22:07 +08:00
|
|
|
[1] Function names for user-level functions should be enclosed with in
|
2002-09-09 15:52:39 +08:00
|
|
|
the PHP_FUNCTION() macro. They should be in lowercase, with words
|
2000-10-18 11:00:07 +08:00
|
|
|
underscore delimited, with care taken to minimize the letter count.
|
2000-12-19 14:22:07 +08:00
|
|
|
Abbreviations should not be used when they greatly decrease the
|
|
|
|
readability of the function name itself.
|
2000-10-18 11:00:07 +08:00
|
|
|
|
|
|
|
Good:
|
|
|
|
'mcrypt_enc_self_test'
|
|
|
|
'mysql_list_fields'
|
|
|
|
|
|
|
|
Ok:
|
|
|
|
'mcrypt_module_get_algo_supported_key_sizes'
|
|
|
|
(could be 'mcrypt_mod_get_algo_sup_key_sizes'?)
|
|
|
|
'get_html_translation_table'
|
|
|
|
(could be 'html_get_trans_table'?)
|
|
|
|
|
|
|
|
Bad:
|
|
|
|
'hw_GetObjectByQueryCollObj'
|
|
|
|
'pg_setclientencoding'
|
2000-12-19 14:22:07 +08:00
|
|
|
'jf_n_s_i'
|
2003-05-23 18:38:43 +08:00
|
|
|
|
2000-12-19 14:22:07 +08:00
|
|
|
[2] If they are part of a "parent set" of functions, that parent should
|
|
|
|
be included in the user function name, and should be clearly related
|
|
|
|
to the parent program or function family. This should be in the form
|
|
|
|
of parent_*.
|
|
|
|
|
|
|
|
A family of 'foo' functions, for example:
|
|
|
|
Good:
|
|
|
|
'foo_select_bar'
|
|
|
|
'foo_insert_baz'
|
|
|
|
'foo_delete_baz'
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2000-12-19 14:22:07 +08:00
|
|
|
Bad:
|
|
|
|
'fooselect_bar'
|
|
|
|
'fooinsertbaz'
|
|
|
|
'delete_foo_baz'
|
|
|
|
|
|
|
|
[3] Function names used by user functions should be prefixed
|
2000-09-11 05:07:18 +08:00
|
|
|
with "_php_", and followed by a word or an underscore-delimited list of
|
1999-04-08 05:05:13 +08:00
|
|
|
words, in lowercase letters, that describes the function. If applicable,
|
|
|
|
they should be declared 'static'.
|
2003-05-23 18:38:43 +08:00
|
|
|
|
2000-12-19 14:22:07 +08:00
|
|
|
[4] Variable names must be meaningful. One letter variable names must be
|
1999-04-08 05:05:13 +08:00
|
|
|
avoided, except for places where the variable has no real meaning or
|
|
|
|
a trivial meaning (e.g. for (i=0; i<100; i++) ...).
|
|
|
|
|
2001-10-14 17:24:37 +08:00
|
|
|
[5] Variable names should be in lowercase. Use underscores to separate
|
1999-04-08 05:05:13 +08:00
|
|
|
between words.
|
|
|
|
|
2004-01-22 04:18:09 +08:00
|
|
|
[6] Method names follow the 'studlyCaps' (also referred to as 'bumpy case'
|
|
|
|
or 'camel caps') naming convention, with care taken to minimize the
|
|
|
|
letter count. The initial letter of the name is lowercase, and each
|
|
|
|
letter that starts a new 'word' is capitalized.
|
|
|
|
|
|
|
|
Good:
|
|
|
|
'connect()'
|
|
|
|
'getData()'
|
|
|
|
'buildSomeWidget()'
|
|
|
|
|
|
|
|
Bad:
|
|
|
|
'get_Data()'
|
|
|
|
'buildsomewidget'
|
|
|
|
'getI()'
|
|
|
|
|
|
|
|
[7] Classes should be given descriptive names. Avoid using abbreviations
|
2003-05-23 18:38:43 +08:00
|
|
|
where possible. Each word in the class name should start with a capital
|
|
|
|
letter, with words underscore delimited. The class name should be prefixed
|
|
|
|
with the name of the 'parent set'.
|
|
|
|
|
|
|
|
Good:
|
|
|
|
'Curl'
|
|
|
|
'Foo_Bar'
|
|
|
|
|
|
|
|
Bad:
|
|
|
|
'foobar'
|
|
|
|
'foo_bar'
|
|
|
|
'FooBar'
|
|
|
|
|
1999-04-08 05:05:13 +08:00
|
|
|
|
|
|
|
Syntax and indentation
|
|
|
|
----------------------
|
|
|
|
|
|
|
|
[1] Never use C++ style comments (i.e. // comment). Always use C-style
|
|
|
|
comments instead. PHP is written in C, and is aimed at compiling
|
|
|
|
under any ANSI-C compliant compiler. Even though many compilers
|
|
|
|
accept C++-style comments in C code, you have to ensure that your
|
|
|
|
code would compile with other compilers as well.
|
|
|
|
The only exception to this rule is code that is Win32-specific,
|
|
|
|
because the Win32 port is MS-Visual C++ specific, and this compiler
|
|
|
|
is known to accept C++-style comments in C code.
|
|
|
|
|
|
|
|
[2] Use K&R-style. Of course, we can't and don't want to
|
2000-11-27 05:45:44 +08:00
|
|
|
force anybody to use a style he or she is not used to, but,
|
1999-04-08 05:05:13 +08:00
|
|
|
at the very least, when you write code that goes into the core
|
|
|
|
of PHP or one of its standard modules, please maintain the K&R
|
|
|
|
style. This applies to just about everything, starting with
|
2001-10-14 17:24:37 +08:00
|
|
|
indentation and comment styles and up to function declaration
|
1999-04-08 05:05:13 +08:00
|
|
|
syntax.
|
2002-01-04 21:14:53 +08:00
|
|
|
|
2004-01-26 20:37:48 +08:00
|
|
|
(see also http://www.catb.org/~esr/jargon/html/I/indent-style.html)
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2000-11-27 05:45:44 +08:00
|
|
|
[3] Be generous with whitespace and braces. Always prefer:
|
|
|
|
|
2002-09-09 15:54:11 +08:00
|
|
|
if (foo) {
|
|
|
|
bar;
|
|
|
|
}
|
2000-11-27 05:45:44 +08:00
|
|
|
|
2002-09-09 15:54:11 +08:00
|
|
|
to:
|
2000-11-27 05:45:44 +08:00
|
|
|
|
2002-09-09 15:54:11 +08:00
|
|
|
if(foo)bar;
|
2000-11-27 05:45:44 +08:00
|
|
|
|
2001-10-14 17:24:37 +08:00
|
|
|
Keep one empty line between the variable declaration section and
|
1999-04-08 05:05:13 +08:00
|
|
|
the statements in a block, as well as between logical statement
|
|
|
|
groups in a block. Maintain at least one empty line between
|
|
|
|
two functions, preferably two.
|
|
|
|
|
2000-11-27 05:45:44 +08:00
|
|
|
[4] When indenting, use the tab character. A tab is expected to represent
|
|
|
|
four spaces. It is important to maintain consistency in indenture so
|
|
|
|
that definitions, comments, and control structures line up correctly.
|
|
|
|
|
2004-01-30 15:53:12 +08:00
|
|
|
[5] Precompiler statements (#if and such) MUST start at column one. To
|
|
|
|
indent preprocessor directives you should put the # at the beginning
|
|
|
|
of a line, followed by any number of whitespace.
|
2004-01-30 15:01:25 +08:00
|
|
|
|
1999-04-08 05:05:13 +08:00
|
|
|
Documentation and Folding Hooks
|
|
|
|
-------------------------------
|
|
|
|
|
|
|
|
In order to make sure that the online documentation stays in line with
|
|
|
|
the code, each user-level function should have its user-level function
|
|
|
|
prototype before it along with a brief one-line description of what the
|
|
|
|
function does. It would look like this:
|
|
|
|
|
|
|
|
/* {{{ proto int abs(int number)
|
2001-10-14 17:24:37 +08:00
|
|
|
Returns the absolute value of the number */
|
2002-09-09 15:52:39 +08:00
|
|
|
PHP_FUNCTION(abs)
|
2000-09-11 05:07:18 +08:00
|
|
|
{
|
1999-04-08 05:05:13 +08:00
|
|
|
...
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
The {{{ symbols are the default folding symbols for the folding mode in
|
2001-06-05 21:12:10 +08:00
|
|
|
Emacs and vim (set fdm=marker). Folding is very useful when dealing with
|
|
|
|
large files because you can scroll through the file quickly and just unfold
|
|
|
|
the function you wish to work on. The }}} at the end of each function marks
|
|
|
|
the end of the fold, and should be on a separate line.
|
1999-04-08 05:05:13 +08:00
|
|
|
|
|
|
|
The "proto" keyword there is just a helper for the doc/genfuncsummary script
|
|
|
|
which generates a full function summary. Having this keyword in front of the
|
|
|
|
function prototypes allows us to put folds elsewhere in the code without
|
|
|
|
messing up the function summary.
|
|
|
|
|
|
|
|
Optional arguments are written like this:
|
|
|
|
|
|
|
|
/* {{{ proto object imap_header(int stream_id, int msg_no [, int from_length [, int subject_length [, string default_host]]])
|
2001-10-14 17:24:37 +08:00
|
|
|
Returns a header object with the defined parameters */
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2000-12-19 14:22:07 +08:00
|
|
|
And yes, please keep the prototype on a single line, even if that line
|
|
|
|
is massive.
|
|
|
|
|
|
|
|
New and Experimental Functions
|
|
|
|
-----------------------------------
|
|
|
|
To reduce the problems normally associated with the first public
|
|
|
|
implementation of a new set of functions, it has been suggested
|
|
|
|
that the first implementation include a file labeled 'EXPERIMENTAL'
|
|
|
|
in the function directory, and that the functions follow the
|
|
|
|
standard prefixing conventions during their initial implementation.
|
|
|
|
|
|
|
|
The file labelled 'EXPERIMENTAL' should include the following
|
|
|
|
information:
|
2001-10-14 17:24:37 +08:00
|
|
|
Any authoring information (known bugs, future directions of the module).
|
2000-12-19 14:22:07 +08:00
|
|
|
Ongoing status notes which may not be appropriate for CVS comments.
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2000-10-18 15:34:56 +08:00
|
|
|
Aliases & Legacy Documentation
|
|
|
|
-----------------------------------
|
|
|
|
You may also have some deprecated aliases with close to duplicate
|
2000-10-18 11:00:07 +08:00
|
|
|
names, for example, somedb_select_result and somedb_selectresult. For
|
2001-10-14 17:24:37 +08:00
|
|
|
documentation purposes, these will only be documented by the most
|
2000-10-18 15:34:56 +08:00
|
|
|
current name, with the aliases listed in the documentation for
|
2000-10-18 11:00:07 +08:00
|
|
|
the parent function. For ease of reference, user-functions with
|
|
|
|
completely different names, that alias to the same function (such as
|
|
|
|
highlight_file and show_source), will be separately documented. The
|
|
|
|
proto should still be included, describing which function is aliased.
|
|
|
|
|
2000-10-18 15:34:56 +08:00
|
|
|
Backwards compatible functions and names should be maintained as long
|
|
|
|
as the code can be reasonably be kept as part of the codebase. See
|
2001-10-14 17:24:37 +08:00
|
|
|
/phpdoc/README for more information on documentation.
|