Typecho PHP encoding specification

If you have decided to contribute code to Typecho, please read the following specifications in detail and strictly comply with them. This can greatly reduce our workload while ensuring the readability of your code.

appointment

Document code

Please adjust your editor file code to UTF-8 , and Close UTF-8 BOM Function of. Do not use the notepad provided with Windows to edit the project file.

indent

The detailed code indentation will be mentioned later. It should be noted here that the code indentation in the Typecho project uses 4 spaces , rather than tab, be sure to adjust.

UNIX encoding specification

If you are writing a php file, you must leave the last blank line according to the UNIX C language coding specification. such as

 <? php
 //this is a test file
 echo  'hello' ;
 <--- Leave this line blank

Also, if this file is a pure php file (no nested HTML), please do not use ?> At the end of the symbol, leave the last line blank.

UNIX Style Wrap

We use UNIX style line breaks here, that is, only line breaks (LF or " n") without carriage returns (CR or " r"), please adjust in your editor

name

File Naming

Typecho file naming adopts the same naming method as Zend Framework, which is also the naming scheme used by most third-party expansion packages. The rule of this naming method is that the class name is associated with the file name. The method of association is to take the directory where the package is located as the root directory, to the file where the class is located, and change the directory separator to the underscore to name the class. For example, our package name is Typecho, and the path of our class file is Typecho/Db/Adapter.php. Then the name of this class is Typecho_Db_Adapter.

Class Naming

Use the camel underline rule and capitalize the first letter.

 class Typecho_Db {

Function (method, interface) naming

Use the camel rule, with the first letter in lower case.

 public  function fetchRows ( Typecho_Db_Query $query ,  array  $filter  =  NULL )

Variable Naming

Use the camel rule, with the first letter in lower case.

 protected  $callbackFunctions ;

If it is a private variable, please underline the variable name.

 private  $_adapter ;

Constant Names

All letters are capitalized, and double underscores are added before and after them. Words are separated by underscores. If they are internal constants of Typecho, they need to be prefixed with TYPECHO.

 define ( '__TYPECHO_DB_ADAPTER__' ,  'Mysql' ) ;

notes

Note is the key point of open source projects, please pay attention to it.

header comment

The header comments are mainly used to describe the copyright, agreement, author and version of this file. For the Typecho core development group, please write it in the following form (you can set it as a code template).

 <? php
 /** * Typecho Blog Platform * * @author     qining * @copyright  Copyright (c) 2008 Typecho team ( http://www.typecho.org ) * @license    GNU General Public License 2.0 * @version    $Id$ */

Where author is the name of the author, please name it yourself. The version is defined as $Id $to match the keywords of svn. Set the svn: keywords attribute of this file to id, and after each submission, $Id $will be replaced with specific version information, such as: $Id: Db.php 14 2008-02-23 13:07:16Z magike.net $.

Reference files and defining constant comments

File references and constant definitions are generally placed at the beginning of the file. For single line notes, refer to the c99 standard.

 /**Define Database Adapter**/
 define ( '__TYPECHO_DB_ADAPTER__' ,  'Mysql' ) ;   /**Database exception**/
 require_once  'Db/Exception.php' ;

Multiline comments, using the following form

 /** *Define database query read/write status *True indicates the read status *False indicates the write status * */
 define ( '__TYPECHO_DB_READ__' ,  true ) ;
 define ( '__TYPECHO_DB_WRITE__' ,  false ) ;

Class (interface) comments

A class (interface) must declare its role when it is declared. If it is a class library file, it must declare its package. This note refers to the phpdoc specification.

 /** *Class containing support methods for obtaining data *__TYPECHO_DB_HOST__, __TYPECHO_DB_PORT__, __TYPECHO_DB_NAME__ must be defined, * __TYPECHO_DB_USER__, __TYPECHO_DB_PASS__, __TYPECHO_DB_CHAR__ * * @package Db */
 class Typecho_Db {

Function (method, interface) comments

Refer to the phpdoc specification for the declaration comments of functions (methods, interfaces). Note that if it is a non return function, @ return void must be specified. Please try to use known types in the function parameter table. If an exception is thrown in the function, @ throws<exception type>must be specified.

 /** *Get all rows at once *  *@ param TypechoDbQuery $query Query object *@ param array $filter row filter function, which takes each query row as the first parameter and passes it into the specified filter * @return array */
 public  function fetchRows ( Typecho_Db_Query $query ,  array  $filter  =  NULL )
 {   /** *Database class constructor *  *@ param string $adapter Database adapter name * @return void * @throws TypechoDbException */
 public  function __construct ( $adapter  = __TYPECHO_DB_ADAPTER__ )
 {

Program interline comment

Double slash annotation method is adopted for interline annotation

 //Instantiate adapter object
 $this -> _adapter =  new  $adapter ( ) ;

Brace Placement

The curly bracket line breaking rule is consistent with the Linux kernel writing rule

 class TypechoDb {
     public  function __construct ( $adapter  = __TYPECHO_DB_ADAPTER__ )
     {
         if  ( ! defined ( $const  =  '__TYPECHO_DB_HOST__' )  || 
         ! defined ( $const  =  '__TYPECHO_DB_PORT__' )  || 
         ! defined ( $const  =  '__TYPECHO_DB_NAME__' )  || 
         ! defined ( $const  =  '__TYPECHO_DB_USER__' )  || 
         ! defined ( $const  =  '__TYPECHO_DB_PASS__' )  ||
         ! defined ( $const  =  '__TYPECHO_DB_CHAR__' ) )  {
         }  else  {
         }

Comma placement

Commas are used to separate parameters in the function. All parameters must be separated from the preceding comma by a space (except the first parameter).

 public  function connect ( $host ,  $port ,  $db ,  $user ,  $password ,  $charset  =  NULL )

Space Use

In addition to using spaces between parameters, all operators should use spaces, including the character connector (.).

 $host  .  ':'  .  $port

Code layout

Class layout

The internal methods of the class are sorted as

 __construct private
 protected
 public __destruct

The sorting of attributes is

 private
 protected
 public

Blank line usage

You can use blank lines to split different blocks of code. Please make suggestions. Please do not use the coding style of one blank line per line.

Print/Export
language
  ?