Skip to main content

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 :)


References: 
http://www.slideshare.net/weierophinney/best-practices-of-php-development-presentation 
http://www.slideshare.net/rayhan_wm/standard-coding-oop-techniques-and-code-reuse 
http://www.yiiframework.com/doc/guide/1.1/en/basics.mvc 

Comments

Popular posts from this blog

PHP Tutorial–Guest Book–System Definition and Requirements

In this lesson I’m going to illustrate the system definition and requirements for the Guest Book application. It is an important step in developing any application, since this step gives us an orientation about the system and its functionality, what is expected from the system to do. actually the system definition comes from keywords from customers and end users, usually try to make it clear for what they need in the application, and what they expect the system to do.  As developers it is our role to define the requirements for the system to be developed. In our example the system definition for the Guest Book is: A system that allows visitors of the site to post their comments and feedbacks about the site, with the possibility for managing comments and maintain it easily and user friendly. On the other hand, the system requirements are: a web server, since it is clear that the developed system is going to run on the Internet, so it is a web-based application not windows-based.

Welcome To my blog :)

Hi all, I'm glad to start blogging here, to keep close to all of those who are interested in technologies in general, and specially programming and developing softwares. From this area, I'm going to share my little experience in developing windows based applications using C# .Net, and web based applications using PHP and ASP.Net.  There'll be lessons and trainings on programming and developing softwares.  Hope you all come and share us with your knowledge and experience, so we'll share knowledge. We all here believe that knowledge for all, and work for that. come and be part of this.  If you want to share us with your knowledge and experience in any topics related to programming and developing softwares, don't hesitate to contact us and start the trip.  Best Regards,  Anas Jaghoub