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-bas...

تعلم تطوير تطبيقات للموبايل باستخدام الأندرويد

مرحبا أصدقائي يسعدني أن أبدأ معكم سلسلة حلقات في تعلم تطوير تطبيقات للموبايل باستخدام الأندرويد ، وسأسعى جاهدا معكم في أن تكون هذه السلسلة من أوائل السلسلات في اللغة العربية لتعليم برمجة تطبيقات الأندرويد من البداية وحتى الاحتراف. وسأحاول قدر المستطاع الشرح بلغة عربية بسيطة ومفهومة. حيث ستكون غدا إن شاء الله الحلقة الأولى من تعلم تطوير تطبيقات الموبايل باستخدام الأندرويد. أنس الجاغوب Twitter: @anasjaghoub Facebook: anasjaghoub

AABU GTUG Opening Event

On February 17, 2011 We’ve headed to Al Al Bayt University, to run one of the biggest event established there, for opening the AABU GTUG (Google’s Technologies Users Group). The event focused on introducing the GTUGS (Google’s Technologies Users Groups) and introducing Google’s technologies to students, such as Android and App Engine, to Chrome Extensions and HTML5 demos. Attendees exceeded 250 attendee, in addition to Vice president of Al Al Bayt University Dr. Hashem Al Masaeed, Deanship of College of I.T at Al Al Bayt University Prof. Ismail Ababneh, and a quite number of professors and teachers at the college of I.T at AABU, in addition to Yarmuk fm and Al Ro’aya  tv. AABU GTUG as a Google’s Technologies Users Group interested in Google’s technologies and tries as possible to increase students awareness about Google’s technologies, and introduce it to them. Their was a lot of activities during the event, beginning with the key note from me Anas Jaghoub, that included introducin...