A few months back, around the second week of January 2014, I had to go through someone's web application. The application itself was working fine, it was made solid, but when I checked the codes… I almost hulked out!!! No doubt, the developer knew his sh*t, he was aware of how to get around PHP. But his codes were not community friendly. So I've decided to gather a few points that might help new PHP developers on how to make your code community friendly and make it look neat and intuitive. [Note: turned out my codes are not that developer friendly too, so I'm going to stick by the rules from now on ;) ]
Yes, Its time for you to get out of your comfort zone and start using OOP. It is going to save you massive amounts of development hours and productivity. So my fellow developers, its time to ditch procedural approach of development and adopt the Object Oriented style.
2. Comment where necessary:
Have you ever had a situation where you write something and after a while when you look at it, you have no idea why it was written. No? Good. But I have had those situations whenever I forget to comment. It is a good practice to comment whenever it is necessary to describe what's going on in that particular line or block of code. But we need to remember, with PHP we develop web based applications so we should keep our applications as light as possible, therefore, use neat and straightforward comments, don't over do it.
//////////////// this is a comment for image upload///////////////// ///////////////// this is comment for validating name //////////////// ////////////////////// it saves image
//upload image //form validation //saves image to folder
3. Start using Frameworks:
Frameworks help your application with a solid backbone. They will speed up your work as they consist libraries that can be called whenever it is necessary. Lets, take an example, If you are writing an application which has form and you need to perform validation on incoming data, you end up writing the validation script. But when you are using a framework, all you do is call the validation library and let it handle the validation. This is just a basic example. Frameworks like Laravel, Symfony 2, Zend 2 provides a developer, so many tools that development on framework has become a standard nowadays.
4. Avoid using $_GET for sensitive data and query:
There is a huge security risk if you pass data and query via the URL. Its not possible to ditch, but whenever you can avoid using $_GET.
5. Always sanitize all $_POST data:
The number one rule on a database driven PHP application is “don't trust user input”. Therefore, if you want your application to pass safe data from your input form, always sanitize and scape user input.
6. Use configuration file for global settings:
Always use one file where you store global settings. Don't use database's username and password whenever required. Store it in one place and call them. That reduces security risk and amount of codes that you have to write.
7. Use PHP's inbuilt features and functions:
PHP has some cool function that you can use out of the box. You can use them with little or without settings and they will make your life much more easier. Here are some functions that I particularly use:
These are just a few examples, which will help a developer when the application is in development phase. There are many features and functions that you can use in production phase.
8. Adopt a naming convention that makes sense:
Name things like class, variables, and function in such a way that it makes sense and gives an idea of its purpose. This is very helpful if you have to go through the application later. Instead of tracing to function, the name will give you the general idea of its purpose.
9. DRY or DON'T REPEAT YOURSELF.
Yes, many developers tend to repeat their codes, DON'T DO THAT!! Try making a function, which can be called whenever, required instead or copying and pasting the whole code. That will make you look smart and your program less complicated.
10. Share your codes.
If you want to have your code tested under various circumstances than share it. Make your codes open so everyone can learn something and the developer community will help you point out your mistakes and give you an idea on how to fix it.
11. CODING > READING
As the title suggests, you will never learn coding by reading, you need to code! The development world is ever changing and to keep updated, reading about the changes is not enough. You need to have the first hand experience. And always have a side project for yourself where you can try out your ideas. Following these simple rules, it will make you look more professional and make your and other fellow developers rules life easier.