Home > Backend Development > PHP Tutorial > How to write elegant and attractive PHP code? A brief discussion on writing standards

How to write elegant and attractive PHP code? A brief discussion on writing standards

青灯夜游
Release: 2023-04-10 16:00:02
forward
3692 people have browsed it

How to write elegant and attractive PHP code? This article will take you through the basic writing specifications and framework specifications of PHP code. Understanding them will make your PHP code more elegant!

How to write elegant and attractive PHP code? A brief discussion on writing standards

Introduction

Today Lao Wang told me that his code is so bad, like a lump of xiang. Ask me how to

improve the quality of my code and make my code more pleasing to the eye and more comfortable, just like the way

my eyes light up when I see a long-legged girl.

So I: You do this first, then this, and then that. . . . . .

Classmate Lao Wang: Stop making trouble, what exactly is going on?

Okay, I'm going to start pretending to be 13. . .

How to write elegant and attractive PHP code? A brief discussion on writing standards

Basic specifications

Let’s talk about the most basic things first:

  • Variables Use camelCase for names. Don’t use pinyin for words you don’t understand. Instead, look up the dictionary to find the corresponding word.

  • Constant names should be named with uppercase letters and underscores. For example: SYSTEM_EROOR = 50000.

  • Use the Tab key for indentation, do not type a bunch of spaces for indentation.

  • The class name is named in camel case with the first letter in capital letters. You need to know the meaning by seeing the name. Comments explain the function of this class. For example:

How to write elegant and attractive PHP code? A brief discussion on writing standards

  • The method name is named in camel case. The number of method lines should be controlled to about 80 lines. Comments should explain what the function is used for.

How to write elegant and attractive PHP code? A brief discussion on writing standards

  • The curly braces occupy one line, for example:

How to write elegant and attractive PHP code? A brief discussion on writing standards

    ##foreach Use quotes with caution. For example, the following code will have problems:

How to write elegant and attractive PHP code? A brief discussion on writing standards

The expected result is the output:

2 4 6, the actual result is 2 4 4, as to why Take a look at my previous article: Do you really understand the & symbol in PHP?. You can use the array_walk` method to avoid this problem, example:

How to write elegant and attractive PHP code? A brief discussion on writing standards

    Avoid
  • if, elesenesting If it is too deep, many nestings can be eliminated by early termination. Here is a simple example:

How to write elegant and attractive PHP code? A brief discussion on writing standards

It is recommended to use the second method and return directly if the conditions are not met. , the rest are symbolic conditions, so you avoid writing a lot of code in if.

  • Multiple

    if/else use switch instead, PHP8.0 version can use match to be more concise. Install the SonarLint

    plug-in in
  • phpstorm. If there are dotted lines in the code you write, it means it is not ideal, then you can modify it according to the prompts. I believe students with obsessive-compulsive disorder will definitely change it, and over time the code will become very standardized. For example:

How to write elegant and attractive PHP code? A brief discussion on writing standards

The method is not used and the method name is not standardized. I have told you that you can modify it quickly or modify it yourself.

Framework specification

  • The above mentioned things are relatively basic, and the following is the main content.

  • I believe many students have used one of the popular frameworks such as

    thinkphplaravelyii.

  • These frameworks are all based on MVC architecture. I have seen many people’s codes and either write business logic in the controller or in the Model. Writing in the Model is better than writing in the Model. The one inside the controller is relatively better. In fact, it is not very friendly to large-scale projects.

  • The following uses the Laravel framework as an example.

Parameter verification

  • API requires parameter verification, but where is the most elegant way to write parameter verification? Many people may define rules in the controller and then call the verification method. Then the verification code will appear in every API, such as what my colleague wrote.

How to write elegant and attractive PHP code? A brief discussion on writing standards

  • This code will appear once in each API. Isn’t it very verbose? So how to solve it?

How to write elegant and attractive PHP code? A brief discussion on writing standards

  • Create a Requsts directory in Laravel's http directory to store the requested parameter verification class. Create a BaseRequest class:

1How to write elegant and attractive PHP code? A brief discussion on writing standards

For example, login requires parameter verification and then create a LoginRequest class to inherit this BaseRequest.

1How to write elegant and attractive PHP code? A brief discussion on writing standards

  • #When using it, just inject this request class into the Controller method.

1How to write elegant and attractive PHP code? A brief discussion on writing standards

#When the request parameters are obtained here, the form will be verified. Otherwise, if the parameter verification fails, the method just defined by the Request accumulation will be called to throw a Json exception and return the information to the customer. end.

Controller

The main workload of the controller is to obtain the request data and return content. It should not do more things, so you can define a Service layer to handle the business. logic. So my controller has only one line of code.

  • Create a Services folder in Laravel's app directory to store the Service class, and create a BaseService class:

1How to write elegant and attractive PHP code? A brief discussion on writing standards

Then Create a UserService to handle user-related business logic.

1How to write elegant and attractive PHP code? A brief discussion on writing standards

Inject this UserService into UserController using:

1How to write elegant and attractive PHP code? A brief discussion on writing standards

Model

Model does not recommend writing business logic. Model is mainly used to define some content and should not manipulate data.

Model's data manipulation should be placed in the Repository, and create a folder Repositories in Laravel's app directory.

Define BaseRepository:

1How to write elegant and attractive PHP code? A brief discussion on writing standards

Define UserRepository for user data related operations, inject UserModel in the constructor:

1How to write elegant and attractive PHP code? A brief discussion on writing standards

Constant

How to define many constants in the project?

Create a Constant directory in the app directory, and then create a Constant class to save these custom constants.

The advantage of this is:

  • Custom constants can be managed centrally.
  • When modifying the constant value, you only need to find and modify it once in this class. The code update is easy to maintain.

1How to write elegant and attractive PHP code? A brief discussion on writing standards

Original address: https://juejin.cn/post/6957290009682509854

Author: ClassmateLin

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to write elegant and attractive PHP code? A brief discussion on writing standards. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:掘金--ClassmateLin
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