Home > Backend Development > PHP Problem > How to write beautiful PHP code

How to write beautiful PHP code

醉折花枝作酒筹
Release: 2023-03-11 09:40:01
forward
1908 people have browsed it

A good coding habit is developed through daily habits. If the team has good coding standards 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 is required to comply with PSR standards. I will only list some commonly used writing methods in coding to explain. For more specifications, you can search PSR standards.

How to write beautiful PHP code

PHP file starting tag

For pure PHP code files, they must start with . As for why, you can find out by yourself if you are interested.

<?php

class TestDemo
{
}
Copy after login

Declaration of the Class class

The class name must be named in camel case (TestDemo) starting with a capital case. The two curly braces are wrapped and occupy one line. At the same time, a multi-line comment is required to describe the creation of the class. People and functions, etc.

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

Constants of the Class class

The constant names in the class must be all capital letters (HELLO_WORLD), and the words are separated by underscores _, and it is best to add comments. Single-line comments are the best Fortunately, add a space after // and then add a comment.

// 声明一个常量
const HELLO_WORLD = &#39;best&#39;;
Copy after login

Member attributes of Class class

Member attributes (also called member variables) in the class, the naming of member attributes can follow three rules: camel case starting with capital ($UserName), Camel case ($userName) and underscore-delimited ($user_name) at the beginning of lowercase; all three naming rules are acceptable. I personally use the underscore-delimited type. You can also choose according to your own habits, but it is best to agree with the team coding rules. Just keep it consistent, and the three modifiers (public, protected, private) cannot be missing.

// 声明一个公共变量
public $user_name = &#39;&#39;;
// 声明一个静态变量
public static $user_age = 18;
Copy after login

Member methods of Class class

The naming of member methods in the class must adopt the camel case naming rule (testAction) starting with lowercase, and the three modifiers of the method (public, protected, private ) must not be missing. Two curly braces should be wrapped and occupy one line. There should be a space on both sides of the equal sign of the parameter. Don’t miss the comments on the method, including the function of the method, parameter description, return value description, etc.

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

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

Operators and expressions

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

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

Standard writing of control structures

Similar to if...else, while, switch...case, foreach, for and other process control structures, they basically need to be combined with brackets () and curly braces { } is used together, requiring a space on both sides of the bracket (), and the left curly brace {needs to go with the right bracket) and have a space, and the closing right curly brace} needs to be on a separate line, and the main content is included in in curly braces {}.

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

Quick one-click formatting

Many IDEs now support one-click formatting of code, such as PhpStorm, etc. The general shortcut keys are Ctrl Alt L, you can also use it according to your own habits Modify shortcut keys and code format standards. 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 easily format the entire file with one click, because it will also format other people’s code. It is recommended to only format your own code. Everyone does not I like others to mess with my code at will, so don't cause trouble! Don't cause trouble! Don't cause trouble! Say important things three times, you know.

Final summary

Let me give you some personal suggestions first, I hope it will be helpful to you:

  • Don’t add extra spaces, also Just add a space if you need to (just follow the code specifications)

  • Delete excess line breaks (affecting the visual beauty of the code)

  • Remember to delete the debugging code in time, not just the comments (I'm afraid 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 it later)

  • You can look down on other people’s code, but don’t modify other people’s code at will (because others won’t feel good about looking at your code)

I just extracted a small part of the commonly used code specifications to talk about. If there is something wrong in the writing, please point it out and I will modify it in time. Thank you. If you have other good coding skills, you are welcome to share them with everyone.

Recommended learning: php video tutorial

The above is the detailed content of How to write beautiful PHP code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template