Why do we need coding standards

A good coding habit is cultivated by usual habits, there are good coding standards in the team, and everyone follows the established coding standards, then I believe that the quality and maintainability of the code will rise to a higher level. At present, PHP requires compliance with the PSR standard specification. I only list some commonly used coding in the description. For more specifications, you can search for the PSR standard.

PHP file start tag

For a pure PHP code file, you must <?php or <? tag, and be on a line alone, and don't add ?> . As for why, if you are interested, you should find out by yourself.

<?php

class TestDemo
{
}

Class declaration

The class name must start with a camel case name (TestDemo) with uppercase letters. The two curly braces wrap and occupy a line by themselves. At the same time, a multi-line comment is needed to explain the creator and role of the class.

/**
 * 这是一个测试Demo类
 * Author: gxcuizy
 * Date: 2021-05-25 13:57:16
 * Class TestClass
 */
class TestDemo
{
}

Class constants

The constant names in the class must be all uppercase letters (HELLO_WORLD), and the words are separated by underscores _ , and it is better to add a comment. It is best // and then add a comment.

// 声明一个常量
const HELLO_WORLD = 'best';    

Member attributes of the Class class

For member attributes (also called member variables) in the class, the naming of member attributes can follow three rules: camel case ($UserName) starting with uppercase, camel case starting with lowercase ($userName), and underscore separated ($user_name); These three naming rules can be used, I personally use underscore separator, you can also choose according to your own habits, but it is best to keep consistent with the team coding rules, and three modifiers (public, protected, private) can not be less.

// 声明一个公共变量
public $user_name = '';
// 声明一个静态变量
public static $user_age = 18;

Member methods of Class

The naming of the member methods in the class must adopt the camel case naming (testAction) rule starting with lowercase, the three modifiers (public, protected, private) of the method must not be less, the two curly braces wrap and occupy a line alone, and the equal sign of the parameter is on both sides One space for each. Don’t miss the method comments, including the function of the method, parameter descriptions, and return value descriptions.

/**
 * 这是一个测试方法
 * @param string $msg 参数说明
 * @return array
 */
public function testAction($msg = '')
{
    // 返回数据格式
    $return = array('code' => 200, 'msg' => '');
    return $return;
}

/**
 * 这是私有方法,方法命名以单下划线开始
 * @param string $arg 参数说明
 * @return string
 */
private function privateAction($arg = '')
{
    return $arg;
}

Operators and expressions

Compared with operators or different types of expressions, no matter where they are used, we need to have a space on both sides of their symbols, such as $a = 1; , 1 + 2 and 1 && 0 .

/**
 * 获取两个数相加的和
 * @param int $one 第一个数
 * @param int $two 第二个数
 * @return int
 */
public function getUserAge($one = 0, $two = 0)
{
    $sum = $one + $two;
    return $sum;
}

Normative writing of control structure

Similar to if …… else , while , switch ……case , foreach , for and other process control structures, they basically need to be used in conjunction with the brackets () and curly brackets {} , requiring that the brackets () ) , and the left curly brackets of the brackets { need to be the same as the right accaaeb14d8 A space, and the closing curly brace } needs a separate line, and the main content is contained in the curly brace {} .

/**
 * 判断用户是否成年
 * @param int $age 年龄
 */
public function logicAction($age = 18)
{
    if ($age >= 18) {
        echo '已成年';
    } else {
        echo '未成年';
    }
}

Quick one-click format

Many IDEs now support one-click formatting codes, such as PhpStorm etc. The general shortcut keys are Ctrl + Alt + L . You can also modify the shortcut keys and code format standards according to your own habits. You can format the entire document with one click or You can select only a certain part of the code for formatting. PS: If this file is not created and modified by you alone, please do not format the entire file easily with one click, because it will format other people's codes. It is recommended to format only your own code, everyone does not Like others to use their own code at will, so don't mess with it! Don't mess up! Don't mess up! The important thing is said three times, you know.

Final summary

Let me give some personal suggestions first, I hope it will help you:

  • Do not add extra spaces, that is, just add one space if you add one (just follow the code specification)
  • Remove redundant line breaks (affect the visual beauty of the code)
  • Remember to delete the debugging code in time, not just comments (for fear that you will forget it over time)
  • Code comments, code comments, code comments (no matter how busy you are, you should write comments appropriately, don’t add them afterwards)
  • You can be uncomfortable with other people's code, but don't modify other people's code at will (because others are uncomfortable looking at your code)
  • Welcome to add...

I just extracted a small part of the commonly used code specifications. If there is something wrong, please point it out. I will revise it in time, thank you. If you have other good coding techniques, you are welcome to share them with you.


gxcuizy
2k 声望287 粉丝

我是如此的平凡,