Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to standardize PHP Code

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

How to carry out PHP code specification, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Although PHP is the best language in the world, the PHP code written is often not the most beautiful. The reason may be that PHP is simple and easy to use and suitable for fast iteration, which causes us to immerse ourselves in the secret delight of rapidly completing the iteration of requirements, but forget the standardization, ignore the ease of maintenance, and dig countless holes for future generations.

[1] A function has written two hundred lines or more

[2] there are seven, eight or even a dozen arguments to a function.

[3] the maximum length of a single line of code / string exceeds 120 characters

[4] thousands of lines are written in a PHP file

[5] when you modify the code, you don't change the corresponding comments.

[6] instead of using the wrapper provided by the web framework, use global variables such as $_ POST,$_GET,$_SESSION directly

In fact, all of the above problems really exist in our project. Writing the above code will not affect the normal operation of the code function, but the so-called predecessors planted trees and future generations to enjoy the cool, messy code is like a pile of weeds, future generations to maintain a pile of weeds is far more painful than a big tree. I am afraid it is difficult to quantify the loss of efficiency. Imagine reading a function with 500 lines in which no less than 50 local variables are defined. when you see a variable, you have no idea what the variable represents, and you have to go back to the place where it is defined. step by step, the train of thought may have been interrupted. If you read a 50-line function, the whole function body can be contained on a computer screen, and you can see it all without even flipping the mouse, how comfortable it will be.

Make a change

I have seen that many development teams will develop their own code specifications when they are aware of the code specification problems. Once we wanted all developers to sit down and negotiate a unified code specification amicably. However, first, it takes a lot of time, second, it is not detailed enough, it is difficult to cover all aspects of coding in the discussion, and the third and most important point, it is impossible to reach an agreement at all. You must have heard of an enduring joke in the program world, that is, whether the code should be indented with spaces or tab. Unfortunately, there is no standard answer to code specification. You can list 10 benefits of indenting spaces, but someone will soon come up with 10 benefits of using tab for indentation. "discussion" doesn't work at all.

Who is the "authority"?

With the above ideas, we need to discuss whose code specification can represent "authority". Usually use PHP to do Web development, you must use a variety of PHP frameworks, such as Laravel,Symfony,YiiFramework,ZendFramework and so on. As a world-renowned open source framework, the code in these frameworks should be very standard.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 243

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report