123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- ========================
- 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 fully
- follow it, but new features are going in that general direction. Many
- sections have been recoded to use these rules.
- Code Implementation
- -------------------
- 0. Document your code in source files and the manual. [tm]
- 1. Functions that are given pointers to resources should not free them
- For instance, ``function int mail(char
- i<100; i++) ...).
- 5. Variable names should be in lowercase. Use underscores to separate
- between words.
- 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. 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. 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 start with a capital letter
- followed by lowercase letters, whereas acronyms and initialisms are written
- according to their standard notation. Usage of acronyms and initialisms is
- not allowed if they are not widely adopted and recognized as such.
- Good:
- 'Curl'
- 'CurlResponse'
- 'HTTPStatusCode'
- 'URL'
- 'BTreeMap' (B-tree Map)
- 'Id' (Identifier)
- 'ID' (Identity Document)
- 'Char' (Character)
- 'Intl' (Internationalization)
- 'Radar' (Radio Detecting and Ranging)
- Bad:
- 'curl'
- 'curl_response'
- 'HttpStatusCode'
- 'Url'
- 'BtreeMap'
- 'ID' (Identifier)
- 'CHAR'
- 'INTL'
- 'RADAR' (Radio Detecting and Ranging)
- 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'.
- PHPAPI char
- Unexposed module function should be static and should not be defined in
- 'php_modulename.h'.
- static int php_session_destroy()
- 2. Main module source file must be named 'modulename.c'.
- 3. Header file that is used by other sources must be named 'php_modulename.h'.
- 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
- force anybody to use a 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.
- Indentstyle: http://www.catb.org/~esr/jargon/html/I/indent-style.html
- 3. Be generous with whitespace and braces. Keep one empty line between the
- variable declaration section and 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. Always prefer::
- if (foo) {
- bar;
- }
- to:
- if(foo)bar;
- 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.
- 5. Preprocessor 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.
- Testing
- -------
- 1. Extensions should be well tested using
|