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!
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. . .
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:
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:
,
elesenesting If it is too deep, many nestings can be eliminated by early termination. Here is a simple example:
if/else use switch instead, PHP8.0 version can use
match to be more concise. Install the
SonarLint
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:
thinkphp丶
laravel丶
yii.
Requsts
directory in Laravel's http directory to store the requested parameter verification class. Create a BaseRequest
class: For example, login requires parameter verification and then create a LoginRequest
class to inherit this BaseRequest
.
#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.
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.
Then Create a UserService to handle user-related business logic.
Inject this UserService into UserController using:
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:
Define UserRepository for user data related operations, inject UserModel in the constructor:
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:
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!