Web Development Best Practices (PHP Best Practices)

Websites development is one of the most challenging projects ever to develop, according to the large number of components you deal with, integrate, and manage in your application. For example, most web projects Deal with databases as their data source, and Integrate with external services called web services and APIs. As well the application Manages its own objects and components from views to models to controllers to plugins and so on.

A skilled web developer should have a great/deep knowledge in most of components used in his system, and as well should have intensive focus on how each component work and how can be used, what can be done, and what cannot.

On the other hand, the huge amount of requirements, components to manage, and time frame given to you to accomplish your mission, and number of people in your team, all these factors together requires from you to organize your tasks, and priorities your requirements, do deep analysis and planning in order to have a high quality product, efficient and delivered on time.

Optimal solutions cannot be achieved all the time, due to many factors, will be discussed in separate articles, but on the other hand, we’ve to provide the most efficient solution with least cost. This applies too to the source code we write.

When more than one developer work on a project, each developer has his imagine, his taste, and his experience, and for sure his mentality. Opening the choice for each developer to apply his taste, experience, and ideas is not always right, and most probably ends with revamping, code fixes, and code rewrite! That’s why there is always standards, headlines and best practices that each web developer has to follow, in order to keep source code clean, maintainable, readable, and efficient. In most cases breaking these standards ends with more bugs, much time spent to fix simple issues, high cost for maintaining the code, and finally revamping the source code again.

Today, I’m going to mention some of the standards, best practices that PHP web developers should follow when writing source code:

1. Ensure documenting your classes/methods, so that any developer will use these classes/methods will have knowledge how to call these methods, what should be passed as parameters, and what are the expected results for calling these classes/methods. It’s recommended as well to use phpDocumentor style of documentation, for e.g:

* @param string $username Username of the user.
* @param string $password Password of the user.

* @return boolean Result of validation

* @author Anas Jaghoub <2013-10-7>

public function isValidUserCredentials($username, $password) {
return $result;


2. Make sure that objects/arrays are not null before accessing their properties/elements. For e.g:
$user = $userTb->getUserByID($id);
echo ‘Welcome: ‘ . $user->name; // bad practice.

If(!empty($user)) {
echo ‘Welcome: ‘ . $user->name; // best practice.


3. Use default parameters as possible in methods. For e.g:
function sum($a = 0, $b = 0) {
return $a + $b;

4. Use built-in array functions as much as possible instead of using your own logic, specially in_array, explode, implode, and array_map. For e.g:
array_map(‘trim’, $array); // best practice.
Instead of a bad practice:
foreach($array as &$arr) {
$arr = trim($arr);


5. Follow a standard naming convention for classes, methods, variables, and constants. For e.g: Class names are MixedCase, functions and variables are camelCase, constants are ALL_UPPER, and non public class members _underscorePrefixed.

6. Make sure that your methods are returning values in all cases, for e.g:
Bad practice
function sum($a, $b) {
if(!empty($a) && !empty($b) {
return $a+b;
The previous function returns ONLY the sum result of $a+$b in case both $a and $b has values. Otherwise nothing will be returned.

7. Follow a standard indentation and styling for your source code, and make sure that all your teammate follow the same style and format.

8. Use inheritance, abstraction, static functions, singletons, factories as much as you can, this will save a lot of time, will make you code reusable, and as well affects the memory consumed.

9. Make sure that your source code is maintainable, sustainable, scalable, readable, and efficient.

10. Keep it simple, and don’t repeat you self.

11. Don’t re-invent the wheel, instead improve it.

To be continued in next article 🙂








Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s