📜 ⬆️ ⬇️

30 harmful tips for php-developers

I did not bother much and paint the obvious facts, but most of the points that scare me in the code are stated here. I tried to make the list particularly concise, so that you don’t have to read it, but the maximum is understandable, so that even beginners understand that it’s impossible to do so. In general, the following is a list of my harmful advice, based on what I encounter in everyday life.



So go:

  1. Be sure to write the layout directly in php-scripts and output it only through echo (for some reason, this construction of the language is needed).
    ')
  2. Try to use as much echo as possible in your projects, remember that it was invented by cool language developers that you are far from.

  3. Prescribe the connection to the database in each file where it is needed, and do not store anywhere about how and what configurations are registered - let everyone else develop to your level.

  4. Create as many connections to the database in different parts of the project as possible, it is desirable that many of them be called at the same time, generally create a new connection to the database each time you start any function.

  5. Do not use the standard functions of the language - write your own, they will work 100 times better and exactly as you need.

  6. Include all project files anywhere in the project, even if you only need one function.

  7. Use the GLOBALS array - there is no way without it, the developers of the language came up with it for your convenience, but the problems with the resulting bluff are the problems of the server, not the language.

  8. Call functions and use loops directly in the layout, this is information output, there are no functions there.

  9. In no case do not use mysql-parameters, write the variables $ _GET, $ _POST, directly to the request, in order not to waste extra CPU time, it is very important for us.

  10. Duplicate the same method in all classes every time you need it, forget about inheriting this relic of the past.

  11. Use only static methods and properties, no need to create objects, classes are not invented for this.

  12. The methods of the classes working with the database should return the RESOURCE and nothing else, it is better to use while every time you receive data from the class.

  13. Be sure to do sql queries in cycles, this is very important, otherwise how do you get all the records from the table?

  14. Store the date and time in fields of type varchar, because the datetime field has some kind of strange format.

  15. Create yourself a field in the table, where for each record you will write through <br /> all the changes with the record, and definitely this way. In no case do not create a separate table of logs, why do we need an extra file on the disk.

  16. Do not create many cells in the database tables, use seialize better and forget about the encoding when saving, if unserialize gives an error every time, just ignore it.

  17. Generally ignore ALL errors in the project, turn off error_reporting - it only hinders, do not be zadrotami.

  18. Never hear, never check a variable for existence, just output it directly to the template, after point 17, Notice is no longer a problem for you.

  19. Write only for($i=0; $i<count($arr); $i++) , because counting the number of elements in the array at each iteration is good, and the postincrement works much faster, and who says the opposite simply does not know what it says. And it is necessary to sort through a large array at first, because it's faster.

  20. Do not use parsing in any way, this is very bad, it is better to download the whole page with the layout and save it in the database directly as received, do not waste time parsing, especially using DomDocument slows down the project very much, and you need to learn it, but you have no time - you need to write a project.

  21. Store all information about users in one table - data for authorization, personal data, home addresses and phone numbers, places of work, but generally keep any information in one table and do not forget about serialize

  22. Do not use indexes and keys in the database, and even more so the connections between the tables, this is very bad.

  23. Never use reference tables, it is better to duplicate everything in one column, especially if the column contains large and frequently repeated data.

  24. Have you heard about the standard for naming functions and methods? Forget it! Who ever thought that there should be some kind of logic in the names of functions, and functions with the names a (), b (), aa () are much shorter to call.

  25. Do functions as much as possible, if a programmer is too lazy to read what a function does - it is a bad programmer.

  26. Never write comments anywhere, because labor programmers do not need them, in the worst case, you can write a comment like / * this is a function a (), it accepts two variables $ a and $ b * / for very stupid ones.

  27. Functions should take as many parameters as possible, do a bunch of different actions with them and always return an unpredictable result in order to get proggers to read their sources.

  28. Since we have already decided not to use inheritance, re-write functions from other classes, we need to supplement this with the fact that the class must be at least 1000 lines, and the more it is, the better.

  29. In order to get a really big class, you need to select a whole line for each bracket, you need to create a lot of class properties, just in case, and we also remember that in no case you can use the standard php functions - we always write our own.

  30. And finally, let's say that you need a script whose actions change depending on the date (and suddenly), be sure to write this date directly to the script so that you can go in and fix it every time with your hands so that you always keep the script under control.

Thanks for attention!

UPD
The publication suddenly gained a negative rating, although all that was set out in it was taken from a real code.
Reply to comments:
From lavkasnov and others who raised this topic
That is what PSR advises.

Of course, but not in cases with cycles and conditions. see PSR-2 5.1-5.6

From CentALT
Dear professionals, tell me how to do the layout correctly so that it does not work out as in 1 and 2?

Create separate template files, which by the way can be connected suddenly without using echo

In general, there are a lot of ways to display information in a template without echo
I for example for such purposes use DOM.
Those. I have a pure html-template in which pseudo-variables are set, and the template engine hooks on the template and knowing how many values ​​the variable has just parses the necessary layout element as many times as necessary, and then through saveHTML displays the result. As a result, we have clean templates, in which only layout, clean scripts in which there is no layout and only 1 echo for the whole project consisting of several hundred pages and happy coders and layout designers, since neither of them is afraid that someone might drop the project or cover layout. For details, I advise you to look at the good old xTemplate (although now there are more powerful counterparts in general with full code separation)

From GMAX
On the prode turned off E_NOTICE
display_errors by itself

Of course, and then you can safely insert non-existent variables right into the template and don’t fool yourself with extra information like Notice, why did you even come up with checks for existence and checks for emptiness, they only complicate the code.

Source: https://habr.com/ru/post/309450/


All Articles