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

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

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

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

سلسلة تعلم برمجة تطبيقات الاندرويد – إنشاء أول تطبيق للاندرويد – الحلقة رقم 2

تحدثنا في الحلقة السابقة عن إعداد بيئة العمل، وأهم الأدوات والحزم اللازمة للبدء في تطوير تطبيقات لنظام التشغيل الاندرويد. سنتحدث في هذه الحلقة عن كيفية إنشاء تطبيق للاندرويد، وما هي مكونات تطبيق الاندرويد والبنية الهيكلية ﻷي تطبيق على الاندرويد. إنشاء تطبيق باستخدام بيئة العمل Netbeans : 1. اضغط على قائمة File 2. اختر New Project. 3. اختر نوع المشروع Android 4. اضغط زر Next. 5. اضبط إعدادات المشروع كالتالي: Project Name: اسم المشروع، يفضل أن يكون اسم المشروع هو نفس اسم التطبيق وذلك لتمييزه عن باقي المشاريع الأخرى Package Name: اسم حزمة المشروع، وهنا يجب اتباع قواعد تسمية الحزم في لغة Java إذ يجب أن تتكون من مقطعين على الأقل ويفصل بين كل مقطع بنقطة، ولا يوجد فراغات. Target Platform: رقم نسخة نظام التشغيل الاندرويد التي يدعمها هذا التطبيق، وهنا لا بد من اختيار أقل رقم يتوافق معه هذا التطبيق لمزيد من التفاصيل شاهد الصورة التالية: 6. اضغط على زر Finish وهكذا نكون قد أنشأنا بمساعدة بيئة العمل Netbeans أول تطبيق للأندرويد حيث تم تصميم هيكل التطبيق، واستدعاء كافة ال...