平方X

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2356|回复: 0

[php] php命名规范

[复制链接]

414

主题

709

帖子

3602

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3602
QQ
发表于 2016-6-30 17:21:24 | 显示全部楼层 |阅读模式
http://php.net/manual/zh/userlandnaming.php
http://php.net/manual/zh/userlandnaming.rules.php
http://git.php.net/?p=php-src.gi ... G_STANDARDS;hb=HEAD
  1. ========================
  2.   PHP Coding Standards
  3. ========================

  4. This file lists several standards that any programmer adding or changing
  5. code in PHP should follow.  Since this file was added at a very late
  6. stage of the development of PHP v3.0, the code base does not (yet) fully
  7. follow it, but it's going in that general direction.  Since we are now
  8. well into version 5 releases, many sections have been recoded to use
  9. these rules.

  10. Code Implementation
  11. -------------------

  12. 0.  Document your code in source files and the manual. [tm]

  13. 1.  Functions that are given pointers to resources should not free them

  14. For instance, ``function int mail(char *to, char *from)`` should NOT free
  15. to and/or from.

  16. Exceptions:

  17. - The function's designated behavior is freeing that resource.  E.g. efree()

  18. - The function is given a boolean argument, that controls whether or not
  19.   the function may free its arguments (if true - the function must free its
  20.   arguments, if false - it must not)

  21. - Low-level parser routines, that are tightly integrated with the token
  22.   cache and the bison code for minimum memory copying overhead.

  23. 2.  Functions that are tightly integrated with other functions within the
  24.     same module, and rely on each other non-trivial behavior, should be
  25.     documented as such and declared 'static'.  They should be avoided if
  26.     possible.

  27. 3.  Use definitions and macros whenever possible, so that constants have
  28.     meaningful names and can be easily manipulated.  The only exceptions
  29.     to this rule are 0 and 1, when used as false and true (respectively).
  30.     Any other use of a numeric constant to specify different behavior
  31.     or actions should be done through a #define.

  32. 4.  When writing functions that deal with strings, be sure to remember
  33.     that PHP holds the length property of each string, and that it
  34.     shouldn't be calculated with strlen().  Write your functions in such
  35.     a way so that they'll take advantage of the length property, both
  36.     for efficiency and in order for them to be binary-safe.
  37.     Functions that change strings and obtain their new lengths while
  38.     doing so, should return that new length, so it doesn't have to be
  39.     recalculated with strlen() (e.g. php_addslashes())

  40. 5.  NEVER USE strncat().  If you're absolutely sure you know what you're doing,
  41.     check its man page again, and only then, consider using it, and even then,
  42.     try avoiding it.

  43. 6.  Use ``PHP_*`` macros in the PHP source, and ``ZEND_*`` macros in the Zend
  44.     part of the source. Although the ``PHP_*`` macro's are mostly aliased to the
  45.     ``ZEND_*`` macros it gives a better understanding on what kind of macro
  46.     you're calling.

  47. 7.  When commenting out code using a #if statement, do NOT use 0 only. Instead
  48.     use "<git username here>_0". For example, #if FOO_0, where FOO is your
  49.     git user foo.  This allows easier tracking of why code was commented out,
  50.     especially in bundled libraries.

  51. 8.  Do not define functions that are not available.  For instance, if a
  52.     library is missing a function, do not define the PHP version of the
  53.     function, and do not raise a run-time error about the function not
  54.     existing.  End users should use function_exists() to test for the
  55.     existence of a function

  56. 9.  Prefer emalloc(), efree(), estrdup(), etc. to their standard C library
  57.     counterparts.  These functions implement an internal "safety-net"
  58.     mechanism that ensures the deallocation of any unfreed memory at the
  59.     end of a request.  They also provide useful allocation and overflow
  60.     information while running in debug mode.

  61.     In almost all cases, memory returned to the engine must be allocated
  62.     using emalloc().

  63.     The use of malloc() should be limited to cases where a third-party
  64.     library may need to control or free the memory, or when the memory in
  65.     question needs to survive between multiple requests.

  66. User Functions/Methods Naming Conventions
  67. ------------------

  68. 1.  Function names for user-level functions should be enclosed with in
  69.     the PHP_FUNCTION() macro. They should be in lowercase, with words
  70.     underscore delimited, with care taken to minimize the letter count.
  71.     Abbreviations should not be used when they greatly decrease the
  72.     readability of the function name itself::

  73.     Good:
  74.     'mcrypt_enc_self_test'
  75.     'mysql_list_fields'

  76.     Ok:
  77.     'mcrypt_module_get_algo_supported_key_sizes'
  78.     (could be 'mcrypt_mod_get_algo_sup_key_sizes'?)
  79.     'get_html_translation_table'
  80.     (could be 'html_get_trans_table'?)

  81.     Bad:
  82.     'hw_GetObjectByQueryCollObj'
  83.     'pg_setclientencoding'
  84.     'jf_n_s_i'

  85. 2.  If they are part of a "parent set" of functions, that parent should
  86.     be included in the user function name, and should be clearly related
  87.     to the parent program or function family. This should be in the form
  88.     of ``parent_*``::

  89.     A family of 'foo' functions, for example:
  90.    
  91.     Good:
  92.     'foo_select_bar'
  93.     'foo_insert_baz'
  94.     'foo_delete_baz'

  95.     Bad:
  96.     'fooselect_bar'
  97.     'fooinsertbaz'
  98.     'delete_foo_baz'

  99. 3.  Function names used by user functions should be prefixed
  100.     with ``_php_``, and followed by a word or an underscore-delimited list of
  101.     words, in lowercase letters, that describes the function.  If applicable,
  102.     they should be declared 'static'.

  103. 4.  Variable names must be meaningful.  One letter variable names must be
  104.     avoided, except for places where the variable has no real meaning or
  105.     a trivial meaning (e.g. for (i=0; i<100; i++) ...).

  106. 5.  Variable names should be in lowercase.  Use underscores to separate
  107.     between words.

  108. 6.  Method names follow the 'studlyCaps' (also referred to as 'bumpy case'
  109.     or 'camel caps') naming convention, with care taken to minimize the
  110.     letter count. The initial letter of the name is lowercase, and each
  111.     letter that starts a new 'word' is capitalized::

  112.     Good:
  113.     'connect()'
  114.     'getData()'
  115.     'buildSomeWidget()'

  116.     Bad:
  117.     'get_Data()'
  118.     'buildsomewidget'
  119.     'getI()'

  120. 7.  Classes should be given descriptive names. Avoid using abbreviations where
  121.     possible. Each word in the class name should start with a capital letter,
  122.     without underscore delimiters (CamelCaps starting with a capital letter).
  123.     The class name should be prefixed with the name of the 'parent set' (e.g.
  124.     the name of the extension)::

  125.     Good:
  126.     'Curl'
  127.     'FooBar'

  128.     Bad:
  129.     'foobar'
  130.     'foo_bar'

  131. Internal Function Naming Conventions
  132. ----------------------

  133. 1.  Functions that are part of the external API should be named
  134.     'php_modulename_function()' to avoid symbol collision. They should be in
  135.     lowercase, with words underscore delimited. Exposed API must be defined
  136.     in 'php_modulename.h'.

  137.     PHPAPI char *php_session_create_id(PS_CREATE_SID_ARGS);

  138.     Unexposed module function should be static and should not be defined in
  139.     'php_modulename.h'.

  140.     static int php_session_destroy()

  141. 2.  Main module source file must be named 'modulename.c'.

  142. 3.  Header file that is used by other sources must be named 'php_modulename.h'.


  143. Syntax and indentation
  144. ----------------------

  145. 1.  Never use C++ style comments (i.e. // comment).  Always use C-style
  146.     comments instead.  PHP is written in C, and is aimed at compiling
  147.     under any ANSI-C compliant compiler.  Even though many compilers
  148.     accept C++-style comments in C code, you have to ensure that your
  149.     code would compile with other compilers as well.
  150.     The only exception to this rule is code that is Win32-specific,
  151.     because the Win32 port is MS-Visual C++ specific, and this compiler
  152.     is known to accept C++-style comments in C code.

  153. 2.  Use K&R-style.  Of course, we can't and don't want to
  154.     force anybody to use a style he or she is not used to, but,
  155.     at the very least, when you write code that goes into the core
  156.     of PHP or one of its standard modules, please maintain the K&R
  157.     style.  This applies to just about everything, starting with
  158.     indentation and comment styles and up to function declaration
  159.     syntax. Also see Indentstyle.

  160.     Indentstyle: http://www.catb.org/~esr/jargon/html/I/indent-style.html

  161. 3.  Be generous with whitespace and braces.  Keep one empty line between the
  162.     variable declaration section and the statements in a block, as well as
  163.     between logical statement groups in a block.  Maintain at least one empty
  164.     line between two functions, preferably two.  Always prefer::

  165.     if (foo) {
  166.         bar;
  167.     }

  168.     to:

  169.     if(foo)bar;

  170. 4.  When indenting, use the tab character.  A tab is expected to represent
  171.     four spaces.  It is important to maintain consistency in indenture so
  172.     that definitions, comments, and control structures line up correctly.

  173. 5.  Preprocessor statements (#if and such) MUST start at column one. To
  174.     indent preprocessor directives you should put the # at the beginning
  175.     of a line, followed by any number of whitespace.

  176. Testing
  177. -------

  178. 1.  Extensions should be well tested using *.phpt tests. Read about that
  179.     in README.TESTING.

  180. Documentation and Folding Hooks
  181. -------------------------------

  182. In order to make sure that the online documentation stays in line with
  183. the code, each user-level function should have its user-level function
  184. prototype before it along with a brief one-line description of what the
  185. function does.  It would look like this::

  186.   /* {{{ proto int abs(int number)
  187.      Returns the absolute value of the number */
  188.   PHP_FUNCTION(abs)
  189.   {
  190.      ...
  191.   }
  192.   /* }}} */

  193. The {{{ symbols are the default folding symbols for the folding mode in
  194. Emacs and vim (set fdm=marker).  Folding is very useful when dealing with
  195. large files because you can scroll through the file quickly and just unfold
  196. the function you wish to work on.  The }}} at the end of each function marks
  197. the end of the fold, and should be on a separate line.

  198. The "proto" keyword there is just a helper for the doc/genfuncsummary script
  199. which generates a full function summary.  Having this keyword in front of the
  200. function prototypes allows us to put folds elsewhere in the code without
  201. messing up the function summary.

  202. Optional arguments are written like this::

  203.   /* {{{ proto object imap_header(int stream_id, int msg_no [, int from_length [, int subject_length [, string default_host]]])
  204.      Returns a header object with the defined parameters */

  205. And yes, please keep the prototype on a single line, even if that line
  206. is massive.

  207. New and Experimental Functions
  208. -----------------------------------
  209. To reduce the problems normally associated with the first public
  210. implementation of a new set of functions, it has been suggested
  211. that the first implementation include a file labeled 'EXPERIMENTAL'
  212. in the function directory, and that the functions follow the
  213. standard prefixing conventions during their initial implementation.

  214. The file labelled 'EXPERIMENTAL' should include the following
  215. information::

  216.   Any authoring information (known bugs, future directions of the module).
  217.   Ongoing status notes which may not be appropriate for Git comments.

  218. In general new features should go to PECL or experimental branches until
  219. there are specific reasons for directly adding it to the core distribution.

  220. Aliases & Legacy Documentation
  221. -----------------------------------
  222. You may also have some deprecated aliases with close to duplicate
  223. names, for example, somedb_select_result and somedb_selectresult. For
  224. documentation purposes, these will only be documented by the most
  225. current name, with the aliases listed in the documentation for
  226. the parent function. For ease of reference, user-functions with
  227. completely different names, that alias to the same function (such as
  228. highlight_file and show_source), will be separately documented. The
  229. proto should still be included, describing which function is aliased.

  230. Backwards compatible functions and names should be maintained as long
  231. as the code can be reasonably be kept as part of the codebase. See
  232. /phpdoc/README for more information on documentation.
复制代码
我是平方X~
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|手机版|Archiver|平方X ( 冀ICP备14018164号 )

GMT+8, 2024-4-25 17:34 , Processed in 0.131890 second(s), 21 queries .

技术支持:Powered by Discuz!X3.4  © 2001-2013 Comsenz Inc.

版权所有:Copyright © 2014-2018 平方X www.pingfangx.com All rights reserved.

快速回复 返回顶部 返回列表