Building a knowledge system-PHP code specification

Source: Internet
Author: User
Building a knowledge system-the first show of PHP code specifications! ----- In the previous blog "building a knowledge system" on PHP code standards, I mentioned a lot of detailed issues. next I will discuss the above blogs in detail: everyone, the specifications of each company may be different. I just want to talk about my specifications! Improve code readability: add comments to the code and read a piece of code without comments. it is like building a knowledge system-PHP code specification
Debut! ----- PHP code specification

The previous blog "building a knowledge system" mentioned a lot of detailed questions. next I will discuss the above blogs in detail:

Each person and company may have different specifications. I just want to talk about my own specifications!

Improve code readability:

  1. Add comments to the code and read a piece of code without comments. it is like reading the scriptures by Tang sengxi, which is hard to understand without passing through!
  2. Add blank lines to the code. the code that is good at using empty lines is like music with a sense of rhythm. there are too many lines and too few lines!
  3. 30-second rule: other programmers fully understand your member function code, which cannot exceed 30 seconds. Otherwise, this code is difficult to maintain.
  4. A line of code should not exceed 120 characters; otherwise, it is better to display a function within the size of a screen, and the maximum length is no more than three screens.
  5. One line of code only does one thing. Similarly, a function is only responsible for one function.
  6. When the loop is large, do not use a meaningful name like $ I.
  7. While, if, else nesting should not exceed three layers, or how to read it in 30 seconds?
  8. The least comments of a function should be useful, all parameters, and all returned values. it is best to comment on the author (Accountability)
  9. All indentation are 4 spaces rather than tabs, and all linefeeds are unix linefeeds. to ensure that 100% is correct, you can enable the IDE function to display blank and invisible characters.


Improve code efficiency and robustness:

  1. If DB-type public processing is not used, use mysql_escape_string to escape the query string before querying the SQL statement.
  2. Define the constant containing the absolute path in the first introduced File. All other introduced file paths start with constants.
  3. All class constants, static variables, and member variables should be stated in advance, and their purposes should be annotated in the declaration, and their values should be initialized to a security value.
  4. If DB-type public processing is not used, use mysql_escape_string to escape the query string before querying the SQL statement.
  5. To call external interfaces and functions, place them in try/catch blocks, check the returned values, perform robust error processing, and record logs.
  6. PHP files are not closed at the end of the PHP file to avoid unnecessary empty lines from being output !!!


Naming rules:

  1. Naming should be meaningful and Annotated. when other programmers read it, they can understand it at a glance, read it, and remember it.
  2. All case-insensitive PHP keywords use lowercase letters, for example, true false null break.
  3. All variables, class member variables, function names, and class member function names are named in the hump format.
  4. All constants, class constants, and static variables use uppercase letters and underlines to separate words.
  5. Static class variables, class member variables, and class member function names of the private and protected attributes start with an underscore
  6. The file name consists of letters, numbers, and underscores. The first character must be a letter. if the file is a class, the file name should be "class. php ", such as" DBConfig. class. php ", otherwise all file names are in lower case, separated by underscores (_), for example," post_list.php"
  7. The class name adopts the Pascal naming rule, that is, all the first letter of a word is capitalized, and the parentheses are separated by a space after the class name.
  8. All class constants are capitalized and words are separated by underscores


Debate

  1. All "{" left braces are followed by spaces and cannot be independent rows.
  2. For Associated arrays, we encourage code to be divided into multiple lines, and the keys of each line are indented to alignment, "=>" left and right

There are a lot of unwritten code. I hope we can provide more suggestions and make our code more and more standardized.

--- This specification is subject to the code specification of Fuzhou Cheyou Network Technology Co., Ltd.

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.