[Transfer] Tips for writing good PHP code

Writing good PHP code is really not easy, here are a few suggestions:

 

1. Use global variables with caution. If global variables are not easy to manage, your code will depend on global variables and the coupling will be too high.

 

2. Be sure not to copy and paste code. Reusable code must be written as functions or classes. When you have more code, you will know how important this is.

Don't hardcode data into the code, it must be configurable. If it is used globally, make a global config. If only used in a class, use the class static variable configuration. Don't write it directly in the code

 

3. The structure of the program should be done well, and a process should be planned first. How the code works should be very clear and have a main line. From A->B->C, it can be understood at a glance, and it is easy to modify the process and add and replace links. Don't A directly enter B and disappear, and then enter C in B and disappear.

 

4. Build a Trace system and add Trace to your code for debugging. Tracking changes to variables can be handy. When publishing code, you can comment out all Traces and just replace them in batches.

 

5. Write more unit test scripts. Once the code is changed, a major version update is made. You don't need to try one by one, just run the unit test to know if something is wrong.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326613380&siteId=291194637