10 things not to do in PHP 7

ByRafael Jaques in

With the launch of PHP7, the opportunity arises to be able to somehow improve some of the practices we have been running.

In this article are gathered some “quirks” that we should leave aside to be able to take advantage of the best that PHP has to offer us.

1.  Do not use functions mysql_ *

Finally the time has come when we will no longer be merely oriented not to use the mysql_ * functions. In PHP 7 these functions have been removed, which means that you will have to switch to the mysqli_ * functions (much better by the way), or use better or more flexible alternatives like PDO or an ORM.

2. Avoid wasting code

In other words, do not write useless code that will waste performance. In fact, the speed in PHP 7 has increased so much that it can hide some problems in the application architecture. Do not be satisfied with the performance of your application just because PHP 7 made it faster.

As a developer, you should always take care of your application so that you only load the necessary resources, optimize code snippets (especially when they are critical), efficiently write database queries, use caching when possible, and so on.

3. Do not use the PHP closing tag at the end of the files

If you take a look at framework codes or large applications such as WordPress, you’ll see that the PHP closing tag is omitted. In fact, Zend Framework Coding Standards prohibit you from doing this. This closing tag is not required, and when you omit it at the end of the file, you are making sure that no whitespace is added at the end of the file (which can lead to errors).

4. Do not go by reference if you do not need it

Sometimes passing by reference is useful and necessary, but on many other occasions it just makes the code more difficult to understand and try to predict the result.

Apparently, some people think that this makes the code faster, which, according to this reference (in English), is not true.

An example of why referrals are bad in PHP is with respect to the shuffle () or sort () functions. Instead of returning the randomized or ordered array, the functions modify the original variable, which makes no sense to me.

5. Do not make bank queries in a loop

Querying the database within a retry is an unnecessary expense. It generates an over processing that is not justified, since you can achieve the same result, more quickly, performing non-repetition queries. When we are into a situation where this is necessary, it is often wiser to query and construct an array of data and then iterate over that array, without having to consult the database.

6. Avoid using * in SQL queries

In fact this is a more database problem, but as we often write queries within PHP code, I think it’s worth it. However, avoid using wildcards (wildcard characters) in SQL queries, especially if your tables have many columns.

Specify exactly the columns you need and retrieve only the columns you need it. This helps to decrease the use of system resources, protects your data and makes things clearer.

Still in SQL subject, know the functions that are available and test the speed as much as you can. When calculating averages, sums or the like, give priority to SQL functions instead of PHP functions. If you are in doubt about the performance of a query, try it out and try other variations – do not forget to use the best one.

7. Do not rely on user input data

It is not wise to trust the data that the user inserts into the application. I always talk about it when it comes to safety. Always filter, sanitize, exhaust, check and be prepared for problems arising from this data. Take a look at my talk on security and defensive programming. There are three specific problems with user data entry: developers do not take into account all possibilities, data is often incorrect, and these data can be intentionally malicious.

A well-designed system can protect itself from all this. Always use functions like filter_var () to check the validity of the data and prepare it for use with a database. In this case it is also interesting to use an ORM and / or Prepared Statements, which will add a layer of filtering to the application.

8. Do not try to be smart

Your goal should be to write an elegant code and to express your intentions as clearly as possible. You may be able to improve that 0.01 second on each page load by shortening all variables to just one letter, using tri-level selectors at various levels and other “expertise”, but that’s nothing compared to both of the headaches you’ll be Causing yourself and everyone around you.

Name your variables appropriately, document your code, and opt for clarity rather than brevity. Better yet, use a standardized object orientation that more or less documents itself and avoids a lot of comments.

9. Do not reinvent the wheel

PHP is already out there a while ago and the sites are still longer. There is a great likelihood that anything you need to do, someone else has done before. Do not be afraid to ask for help. Github is your friend. Composer is your friend. Packagist is also your friend.

From loggers to color manipulation tools, from debuggers to unit test frameworks, from Mailchimp APIs to Twitter Bootstrap, everything is available at the push of a button (or typing in a command). Use it!

10. Do not neglect the other languages

If you are a PHP programmer, it is standard practice to know something about HTML, CSS, JavaScript, and some database (it’s the bare minimum)! When you have a good domain in these languages, it’s time to learn JavaScript again. JavaScript is not jQuery. You must know the language to use it in an efficient way.

I recommend learning all you can about PHP object-oriented. It can save your life and make you program better exponentially. It will also open other languages ​​like C # and Java, as they will be much easier to learn if you have OOP on your utility belt.

Advance more in knowledge by learning about package managers, build scripts, CoffeeScript, LESS, SASS, YAML, template engines and other fantastic tools. I also recommend that you study frameworks (there are several good options out there like Zend Framework and Laravel).

When you’re getting on well with all this, what do you think about learning Python, Ruby, Ruby on Rails, development for Android, iOS and so forth? You must leave your prejudices aside. You may think this is not good for you because you are out of your comfort zone, the things you need for your job. But that’s just the point! Each language has something useful and a little more knowledge does not hurt anyone. It is not by chance that the best PHP programmers we know have knowledge in other programming languages.

READY TO GO! Let PHP 7 come!

I hope you have enjoyed the post and that, in a way, can help you improve your development practices.

Leave a comment! 1

1 comentário

Comentários

Your email address will not be published. Required fields are marked *

Commenting asAnônimo

read more