2019-05-19 03:39:19 +08:00
|
|
|
# PHP coding standards
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2021-05-31 03:32:40 +08:00
|
|
|
This file lists standards that any programmer adding or changing code in
|
|
|
|
PHP should follow. The code base does not yet fully follow it, but new
|
|
|
|
features are going in that general direction. Many sections have been
|
|
|
|
rewritten to comply with these rules.
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2019-05-19 03:39:19 +08:00
|
|
|
## Code implementation
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2019-05-19 03:39:19 +08:00
|
|
|
1. Document your code in source files and the manual. (tm)
|
2002-08-13 17:42:51 +08:00
|
|
|
|
2024-09-27 04:06:17 +08:00
|
|
|
1. PHP is implemented in C11.
|
2024-07-18 17:40:58 +08:00
|
|
|
For instance, the optional fixed-width integers from
|
2023-02-20 21:24:06 +08:00
|
|
|
stdint.h (int8_t, int16_t, int32_t, int64_t and their unsigned
|
2024-07-18 17:40:58 +08:00
|
|
|
counterparts) are supposed to be available.
|
2023-02-20 21:24:06 +08:00
|
|
|
|
2022-10-22 01:25:11 +08:00
|
|
|
1. Functions that are given pointers to resources should not free them.
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2021-05-31 03:32:40 +08:00
|
|
|
For instance, `function int mail(char *to, char *from)` should NOT free `to`
|
|
|
|
and/or `from`.
|
2015-02-08 00:30:07 +08:00
|
|
|
|
2019-05-19 03:39:19 +08:00
|
|
|
Exceptions:
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2019-05-19 03:39:19 +08:00
|
|
|
* The function's designated behavior is freeing that resource. E.g.
|
|
|
|
`efree()`
|
2007-11-12 00:30:35 +08:00
|
|
|
|
2019-05-19 03:39:19 +08:00
|
|
|
* The function is given a boolean argument, that controls whether or not the
|
2021-05-31 03:32:40 +08:00
|
|
|
function may free its arguments (if true, the function must free its
|
|
|
|
arguments; if false, it must not)
|
2007-11-12 00:30:35 +08:00
|
|
|
|
2019-05-19 03:39:19 +08:00
|
|
|
* Low-level parser routines, that are tightly integrated with the token
|
|
|
|
cache and the bison code for minimum memory copying overhead.
|
2007-11-12 00:30:35 +08:00
|
|
|
|
2022-10-22 01:25:11 +08:00
|
|
|
1. Functions that are tightly integrated with other functions within the same
|
2021-05-31 03:32:40 +08:00
|
|
|
module, and rely on each other's non-trivial behavior, should be documented as
|
2019-05-19 03:39:19 +08:00
|
|
|
such and declared `static`. They should be avoided if possible.
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2022-10-22 01:25:11 +08:00
|
|
|
1. Use definitions and macros whenever possible, so that constants have
|
2021-05-31 03:32:40 +08:00
|
|
|
meaningful names and can be easily manipulated. Any use of a numeric
|
|
|
|
constant to specify different behavior or actions should be done through
|
|
|
|
a `#define`.
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2022-10-22 01:25:11 +08:00
|
|
|
1. When writing functions that deal with strings, be sure to remember that PHP
|
2019-05-19 03:39:19 +08:00
|
|
|
holds the length property of each string, and that it shouldn't be
|
|
|
|
calculated with `strlen()`. Write your functions in 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 recalculated with `strlen()` (e.g. `php_addslashes()`).
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2022-10-22 01:25:11 +08:00
|
|
|
1. 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.
|
|
|
|
|
2022-10-22 01:25:11 +08:00
|
|
|
1. Use `PHP_*` macros in the PHP source, and `ZEND_*` macros in the Zend part of
|
2019-05-19 03:39:19 +08:00
|
|
|
the source. Although the `PHP_*` macros 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
|
|
|
|
2022-10-22 01:25:11 +08:00
|
|
|
1. Do not define functions that are not available. For instance, if a library is
|
2019-05-19 03:39:19 +08:00
|
|
|
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
|
|
|
|
2022-10-22 01:25:11 +08:00
|
|
|
1. Prefer `emalloc()`, `efree()`, `estrdup()`, etc. to their standard C library
|
2019-05-19 03:39:19 +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.
|
2002-10-11 01:03:49 +08:00
|
|
|
|
2019-05-19 03:39:19 +08:00
|
|
|
In almost all cases, memory returned to the engine must be allocated using
|
|
|
|
`emalloc()`.
|
2002-10-11 01:03:49 +08:00
|
|
|
|
2019-05-19 03:39:19 +08:00
|
|
|
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.
|
2002-10-11 01:03:49 +08:00
|
|
|
|
2023-02-22 22:13:57 +08:00
|
|
|
1. The return type of "is" or "has" style functions should be `bool`,
|
|
|
|
which return a "yes"/"no" answer. `zend_result` is an appropriate
|
|
|
|
return value for functions that perform some operation that may
|
|
|
|
succeed or fail.
|
|
|
|
|
2019-05-19 03:39:19 +08:00
|
|
|
## User functions/methods naming conventions
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2019-05-19 03:39:19 +08:00
|
|
|
1. Function names for user-level functions should be enclosed with in the
|
|
|
|
`PHP_FUNCTION()` macro. They should be in lowercase, with words underscore
|
|
|
|
delimited, with care taken to minimize the letter count. 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:
|
2019-05-19 03:39:19 +08:00
|
|
|
|
|
|
|
```php
|
|
|
|
str_word_count
|
|
|
|
array_key_exists
|
|
|
|
```
|
2000-10-18 11:00:07 +08:00
|
|
|
|
|
|
|
Ok:
|
2019-05-19 03:39:19 +08:00
|
|
|
|
|
|
|
```php
|
|
|
|
date_interval_create_from_date_string
|
|
|
|
// Could be 'date_intvl_create_from_date_str'?
|
|
|
|
get_html_translation_table()
|
|
|
|
// Could be 'html_get_trans_table'?
|
|
|
|
```
|
2000-10-18 11:00:07 +08:00
|
|
|
|
|
|
|
Bad:
|
2003-05-23 18:38:43 +08:00
|
|
|
|
2019-05-19 03:39:19 +08:00
|
|
|
```php
|
|
|
|
hw_GetObjectByQueryCollObj
|
|
|
|
pg_setclientencoding
|
|
|
|
jf_n_s_i
|
|
|
|
```
|
2007-11-12 00:30:35 +08:00
|
|
|
|
2022-10-22 01:25:11 +08:00
|
|
|
1. If they are part of a "parent set" of functions, that parent should be
|
2019-05-19 03:39:19 +08:00
|
|
|
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:
|
2018-10-13 20:17:28 +08:00
|
|
|
|
2000-12-19 14:22:07 +08:00
|
|
|
Good:
|
2019-05-19 03:39:19 +08:00
|
|
|
|
|
|
|
```php
|
|
|
|
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:
|
|
|
|
|
2019-05-19 03:39:19 +08:00
|
|
|
```php
|
|
|
|
fooselect_bar
|
|
|
|
fooinsertbaz
|
|
|
|
delete_foo_baz
|
|
|
|
```
|
2003-05-23 18:38:43 +08:00
|
|
|
|
2022-10-22 01:25:11 +08:00
|
|
|
1. Function names used by user functions should be prefixed with `_php_`, and
|
2019-05-19 03:39:19 +08:00
|
|
|
followed by a word or an underscore-delimited list of words, in lowercase
|
|
|
|
letters, that describes the function. If applicable, they should be declared
|
|
|
|
`static`.
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2022-10-22 01:25:11 +08:00
|
|
|
1. Variable names must be meaningful. One letter variable names must be avoided,
|
2019-05-19 03:39:19 +08:00
|
|
|
except for places where the variable has no real meaning or a trivial
|
|
|
|
meaning (e.g. `for (i=0; i<100; i++) ...`).
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2022-10-22 01:25:11 +08:00
|
|
|
1. Variable names should be in lowercase. Use underscores to separate between
|
2019-05-19 03:39:19 +08:00
|
|
|
words.
|
|
|
|
|
2022-10-22 01:25:11 +08:00
|
|
|
1. Method names follow the *studlyCaps* (also referred to as *bumpy case* or
|
2019-05-19 03:39:19 +08:00
|
|
|
*camel caps*) naming convention, with care taken to minimize the letter
|
|
|
|
count. The initial letter of the name is lowercase, and each letter that
|
2024-05-09 02:35:44 +08:00
|
|
|
starts a new "word" is capitalized.
|
2004-01-22 04:18:09 +08:00
|
|
|
|
2024-05-09 02:35:44 +08:00
|
|
|
1. Class names should be descriptive nouns in *PascalCase* and as short as
|
|
|
|
possible. Each word in the class name should start with a capital letter,
|
|
|
|
without underscore delimiters. The class name should be prefixed with the
|
|
|
|
name of the "parent set" (e.g. the name of the extension) if no namespaces
|
|
|
|
are used.
|
|
|
|
|
|
|
|
1. Abbreviations and acronyms as well as initialisms should be avoided wherever
|
|
|
|
possible, unless they are much more widely used than the long form (e.g. HTTP
|
|
|
|
or URL). Abbreviations, acronyms, and initialisms should be treated like
|
|
|
|
regular words, thus they should be written with an uppercase first character,
|
|
|
|
followed by lowercase characters.
|
|
|
|
|
|
|
|
1. Diverging from this policy is allowed to keep internal consistency within a
|
|
|
|
single extension, if the name follows an established, language-agnostic
|
|
|
|
standard, or for other reasons, if those reasons are properly justified
|
|
|
|
and voted on as part of the RFC process.
|
|
|
|
|
|
|
|
|
|
|
|
Good method names:
|
2019-05-19 03:39:19 +08:00
|
|
|
|
|
|
|
```php
|
|
|
|
connect()
|
|
|
|
getData()
|
|
|
|
buildSomeWidget()
|
2024-05-09 02:35:44 +08:00
|
|
|
performHttpRequest()
|
2019-05-19 03:39:19 +08:00
|
|
|
```
|
2004-01-22 04:18:09 +08:00
|
|
|
|
2024-05-09 02:35:44 +08:00
|
|
|
Bad method names:
|
2004-01-22 04:18:09 +08:00
|
|
|
|
2019-05-19 03:39:19 +08:00
|
|
|
```php
|
|
|
|
get_Data()
|
|
|
|
buildsomewidget()
|
|
|
|
getI()
|
2024-05-09 02:35:44 +08:00
|
|
|
performHTTPRequest()
|
2019-05-19 03:39:19 +08:00
|
|
|
```
|
|
|
|
|
2024-05-09 02:35:44 +08:00
|
|
|
Good class names:
|
2019-05-19 03:39:19 +08:00
|
|
|
|
|
|
|
```php
|
|
|
|
Curl
|
|
|
|
CurlResponse
|
2024-05-09 02:35:44 +08:00
|
|
|
HttpStatusCode
|
|
|
|
Url
|
|
|
|
BtreeMap // B-tree Map
|
|
|
|
UserId // User Identifier
|
2019-05-19 03:39:19 +08:00
|
|
|
Char // Character
|
|
|
|
Intl // Internationalization
|
2024-05-09 02:35:44 +08:00
|
|
|
Ssl\Certificate
|
|
|
|
Ssl\Crl // Certificate Revocation List
|
|
|
|
Ssl\CrlUrl
|
2019-05-19 03:39:19 +08:00
|
|
|
```
|
2003-05-23 18:38:43 +08:00
|
|
|
|
2024-05-09 02:35:44 +08:00
|
|
|
Bad class names:
|
2014-01-26 17:31:27 +08:00
|
|
|
|
2019-05-19 03:39:19 +08:00
|
|
|
```php
|
|
|
|
curl
|
|
|
|
curl_response
|
2024-05-09 02:35:44 +08:00
|
|
|
HTTPStatusCode
|
|
|
|
URL
|
|
|
|
BTreeMap
|
|
|
|
UserID // User Identifier
|
2019-05-19 03:39:19 +08:00
|
|
|
CHAR
|
|
|
|
INTL
|
2024-05-09 02:35:44 +08:00
|
|
|
SSL\Certificate
|
|
|
|
SSL\CRL
|
|
|
|
SSL\CRLURL
|
2019-05-19 03:39:19 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
## Internal function naming conventions
|
|
|
|
|
|
|
|
1. Functions that are part of the external API should be named
|
|
|
|
`php_modulename_function()` to avoid symbol collision. They should be in
|
|
|
|
lowercase, with words underscore delimited. Exposed API must be defined in
|
|
|
|
`php_modulename.h`.
|
|
|
|
|
|
|
|
```c
|
2014-01-26 17:31:27 +08:00
|
|
|
PHPAPI char *php_session_create_id(PS_CREATE_SID_ARGS);
|
2019-05-19 03:39:19 +08:00
|
|
|
```
|
2014-01-26 17:31:27 +08:00
|
|
|
|
|
|
|
Unexposed module function should be static and should not be defined in
|
2019-05-19 03:39:19 +08:00
|
|
|
`php_modulename.h`.
|
2014-01-26 17:31:27 +08:00
|
|
|
|
2019-05-19 03:39:19 +08:00
|
|
|
```c
|
2015-12-25 18:13:28 +08:00
|
|
|
static int php_session_destroy()
|
2019-05-19 03:39:19 +08:00
|
|
|
```
|
2014-01-26 17:31:27 +08:00
|
|
|
|
2022-10-22 01:25:11 +08:00
|
|
|
1. Main module source file must be named `modulename.c`.
|
2014-01-26 17:31:27 +08:00
|
|
|
|
2022-10-22 01:25:11 +08:00
|
|
|
1. Header file that is used by other sources must be named `php_modulename.h`.
|
2014-01-26 17:31:27 +08:00
|
|
|
|
2019-05-19 03:39:19 +08:00
|
|
|
## Syntax and indentation
|
2014-01-26 17:31:27 +08:00
|
|
|
|
2022-10-22 01:25:11 +08:00
|
|
|
1. Use K&R-style. Of course, we can't and don't want to force anybody to use a
|
2019-05-19 03:39:19 +08:00
|
|
|
style he or she is not used to, but, 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
|
|
|
|
indentation and comment styles and up to function declaration syntax. Also
|
|
|
|
see [Indentstyle](http://www.catb.org/~esr/jargon/html/I/indent-style.html).
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2022-10-22 01:25:11 +08:00
|
|
|
1. Be generous with whitespace and braces. Keep one empty line between the
|
2007-11-12 00:30:35 +08:00
|
|
|
variable declaration section and the statements in a block, as well as
|
2019-05-19 03:39:19 +08:00
|
|
|
between logical statement groups in a block. Maintain at least one empty
|
|
|
|
line between two functions, preferably two. Always prefer:
|
2000-11-27 05:45:44 +08:00
|
|
|
|
2019-05-19 03:39:19 +08:00
|
|
|
```c
|
2002-09-09 15:54:11 +08:00
|
|
|
if (foo) {
|
|
|
|
bar;
|
|
|
|
}
|
2019-05-19 03:39:19 +08:00
|
|
|
```
|
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
|
|
|
|
2019-05-19 03:39:19 +08:00
|
|
|
```c
|
2002-09-09 15:54:11 +08:00
|
|
|
if(foo)bar;
|
2019-05-19 03:39:19 +08:00
|
|
|
```
|
|
|
|
|
2022-10-22 01:25:11 +08:00
|
|
|
1. When indenting, use the tab character. A tab is expected to represent four
|
2021-05-31 03:32:40 +08:00
|
|
|
spaces. It is important to maintain consistency in indentation so that
|
2019-05-19 03:39:19 +08:00
|
|
|
definitions, comments, and control structures line up correctly.
|
|
|
|
|
2022-10-22 01:25:11 +08:00
|
|
|
1. Preprocessor statements (`#if` and such) MUST start at column one. To indent
|
2019-05-19 03:39:19 +08:00
|
|
|
preprocessor directives you should put the `#` at the beginning of a line,
|
2021-05-31 03:32:40 +08:00
|
|
|
followed by any number of spaces.
|
2019-05-19 03:39:19 +08:00
|
|
|
|
2022-10-22 01:25:11 +08:00
|
|
|
1. The length of constant string literals should be calculated via ``strlen()``
|
|
|
|
instead of using ``sizeof()-1`` as it is clearer and any modern compiler
|
|
|
|
will optimize it away. Legacy usages of the latter style exists within the
|
|
|
|
codebase but should not be refactored, unless larger refactoring around that
|
|
|
|
code is taking place.
|
|
|
|
|
2019-05-19 03:39:19 +08:00
|
|
|
## Testing
|
|
|
|
|
2021-05-31 03:32:40 +08:00
|
|
|
1. Extensions should be well tested using `*.phpt` tests. Read more at
|
2019-05-19 03:39:19 +08:00
|
|
|
[qa.php.net](https://qa.php.net/write-test.php) documentation.
|
|
|
|
|
|
|
|
## New and experimental functions
|
2005-12-23 04:24:46 +08:00
|
|
|
|
2019-05-19 03:39:19 +08:00
|
|
|
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.
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2019-05-19 03:39:19 +08:00
|
|
|
The file labelled `EXPERIMENTAL` should include the following information:
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2019-05-19 03:39:19 +08:00
|
|
|
* Any authoring information (known bugs, future directions of the module).
|
|
|
|
* Ongoing status notes which may not be appropriate for Git comments.
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2021-05-31 03:32:40 +08:00
|
|
|
In general, new features should go to PECL or experimental branches until there
|
|
|
|
are specific reasons for directly adding them to the core distribution.
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2019-05-19 03:39:19 +08:00
|
|
|
## Aliases & legacy documentation
|
|
|
|
|
|
|
|
You may also have some deprecated aliases with close to duplicate names, for
|
|
|
|
example, `somedb_select_result` and `somedb_selectresult`. For documentation
|
|
|
|
purposes, these will only be documented by the most current name, with the
|
|
|
|
aliases listed in the documentation for 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
|
2020-06-28 16:44:54 +08:00
|
|
|
documented.
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2019-05-19 03:39:19 +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 the `README` in the
|
|
|
|
PHP documentation repository for more information on documentation.
|