PHP code, file naming specification

Source: Internet
Author: User

Naming conventions

The following naming conventions should be followed as far as possible during development:

  • Class files are suffixed with. class.php (this refers to the class library file used internally by thinkphp, which does not represent an externally loaded class library file), named with the hump method, and capitalized, for example DbMysql.class.php ;
  • The namespace address of the class is the same as the path address, for example, Home\Controller\UserController the path where the class is located should be Application/Home/Controller/UserController.class.php ;
  • Ensure that file naming and invocation are case-sensitive, because the case sensitivity is on top of unix-like systems (while thinkphp is checked for capitalization even on Windows platforms under Debug mode);
  • The class name and file name are the same (including the capitalization of the above), such as the file name of the class is, the name of the Infomodel class is, and the class name of the UserController UserController.class.php InfoModel.class.php Different Class library has a certain specification;
  • Other class library files, such as functions, configuration files, are generally used .php as suffixes (non-requirements introduced by third parties);
  • The name of the function is used in lowercase letters and underscores, for example get_client_ip ;
  • Methods are named using the Hump method, and the first letter is lowercase or the underscore "_" is used, for example, the getUserName _parseType method that usually begins with an underscore is a private method;
  • Properties are named using the Hump method, and the first letter is lowercase or the underscore "_" is used, for example, a property that is tableName _instance usually preceded by an underscore is a private property;
  • A function or method preceded by a double underscore "__" as a magical method, for example __call __autoload ;
  • Constants are named with uppercase letters and underscores, for example, HAS_ONE and MANY_TO_MANY ;
  • Configuration parameters are named with uppercase letters and underscores, for example HTML_CACHE_ON ;
  • Language variables are named with uppercase letters and underscores, for example MY_LANG , language variables that begin with an underscore are typically used in system language variables, for example _CLASS_NOT_EXIST_ ;
  • There is no mandatory specification for the naming of variables, which can be carried out according to the team specification;
  • The thinkphp template file is suffixed by default .html (can be modified by configuration);
  • Data tables and fields are named in lowercase and underlined, and note that field names do not begin with an underscore, such as that think_user tables and user_name fields are correctly written, and data table fields like _username this may be filtered.

Process Statement Specification

The curly braces for all the process statements occupy a single line. Reason: If you encounter more complex business logic, the curly braces will appear a lot of nesting, so we will confuse each of the corresponding curly braces!

1. Branch Statements

123456789101112131415161718192021222324252627282930313233 if($age >= 18 && $age <= 30) {  echo ‘young man‘;}else if($age > 30 && $age <= 60){  echo ‘middle aged‘;}else{  echo ‘old man‘;} //下面这段代码高手我们一个问题,在if语句中,即使在可以不要花括号的情况下,花括号也是要写上的if($age > 60){  echo ‘I am very old‘;}switch($status){  case ‘forbiden‘    echo ‘login forbidden‘;    break;      case ‘normal‘:    echo ‘login in‘;    break;      default:    echo ‘status is wrong‘     break;}

2. Circular Statements

1234567891011121314151617181920 while($condition){  statesments......;}foreach($arrayList as $arrayKey => $arrayItem){  states......;}do{  statements......;  }while($condition)for($start; condition; changenumber){  statements......;}
General rules of Compliance

In the process of development, the following rules are followed to make your development easier:

  • Follow the Framework's naming conventions and catalog specifications;
  • In the development process as far as possible to open debugging mode, early detection problems;
  • Develop a good habit of using the I function to get input variables;
  • After updating or environment change, the first problem is emptying the runtime directory;

PHP code, file naming specification

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.