• notice
  • Congratulations on the launch of the Sought Tech site

Personally summarize some development habits and php code in php development

Introduction Record some necessary error logs, such as failure to write files, failure to write memcache, failure of socket connection, failure to read and write database, log can help quickly locate problems when there is a problem, I personally strongly recommend closing all error reports in the external production environment


1. Accurately understand various concepts. Now new things are emerging one after another, and it is harmful to the development work if the literal meaning and ignorance are not good;//For example, I have encountered someone who understands the concept of loose coupling (this thing is not new) actually requires that the code does not have blank lines, otherwise your program will be "too much" loose"

2. The code is beautiful, with appropriate blank lines, indents, and spaces, which makes it easier to understand the meaning of the code segment

3. The reference type of PHP is very efficient, and it is recommended to use it when performing complex operations

4. It is best to use set_error_handler and set_exception_handler, which makes your project more perfect

5. Autoload is a very flexible thing

6. Use absolute paths to find files as much as possible

7. PHP constants are a very good thing. Many open source projects use an entire file to define the constants to be used.

8. require, require_once, include, include_once have slightly different application scenarios

9. Don't die easily, especially inside the method

10. Replace global $var with $GLOBALS['var']

11. When AJAX transmits data, do not directly json_encode the array found in the database and pass it to the client. This not only has certain security risks (field name exposure), but also wastes bandwidth when some unnecessary data is transmitted. The same applies to the API interface

12. Read more excellent code of online open source projects (not open source code of excellent projects), and learn from them

13. Try to minimize the number of if else nesting levels. Maybe you want to express a very complex logic algorithm, but doing so will at least make the code logic clearer

14. Catching exceptions with try and catch is helpful to the robustness of the code. It is often encountered in the API interface, which is much more friendly.

15. Record some necessary error logs, such as failure to write files, failure to write memcache, failure of socket connection, failure to read and write database, log can help quickly locate problems when there is a problem, I personally strongly recommend closing all error reports in the external production environment

16. Don't only deal with errors, but ignore warnings and notices, which may lead to inexplicable problems in the future. The project should be error_reporting ( E_ALL ^ E_NOTICE ) in the development state, and all errors should be closed when the production environment of the external network is released. reporting display_errors=Off,error_reporting(0)

17. Variables must be initialized

18. Don’t copy and paste. Even if you want to use ready-made code, you should review it line by line before adding it to a new project, because experience tells us that this is too error-prone. need

19. Static methods, class access rights, interfaces, and abstract classes should be used together to give full play to their respective characteristics;

20. Be sure to write comments, and they must be properly commented, otherwise the maintenance work or the person who takes over the code will cry.

21. If you use array_push() to add a unit to the array, it is better to use $array[] =, because there is no extra burden of calling the function (pro-test $array[] = can save more than 60% more time than array_push() )

22. The array_sum() function returns the sum of all the values in the array, which is much faster than calculating the sum of the values in the array in the php array loop



Tags

Technical otaku

Sought technology together

Related Topic

0 Comments

Leave a Reply

+