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 

 

MVC Best Practices, (Tweaking Yii Framework)

Today, I’m going to talk about MVC, what is it, why we should use it in web development, then we’ll dig a bit to Yii framework, to see how does it work, and talk about best practices of MVC’s and Yii framework as well. On the other hand will talk about tweaking Yii framework to speed up a website performance.

What is MVC?

  • MVC is not only a 3 terms combined together.
  • MVC is a design pattern, a software architecture, a standard used by developers to organize high scale projects.
  • The main idea behind using MVC: code re-usability, separation of concerns, modularity, scalability, and maintainability.
  • MVC is a Model, View, Controller. where the model represents the data layer, the view represents the presentation layer, and the controller responsible for handling requests, managing models/views.

     

    What is Yii?

    • Yii is a high performance, component based MVC framework, for developing web applications using PHP.
    • Yii, one of the fastest/efficient/lightweight/feature-rich frameworks.
    • Yii can be used for developing any type of web applications, because it is lightweight and supports caching.
    • Yii is purely implemented using OOP.
    • http://www.yiiframework.com/

     Why Yii?

    • Implements the MVC.
    • Intensively follows OOA/OOP standards, such as (Classes, Objects, Abstracts, Interfaces, Name spaces, Inheritance, Polymorphism, Static functions, Over-riding, Singletons, Factories, Black boxes…etc).
    • Has a useful documentation, and all its classes/functions fully documented with code samples.
    • Supported widely by community, and has a lot of extensions, widgets, that helps developers doing prototypes easily and in minimum time.
    • Open Source, Free to use,share and update.

    How Does Yii Works?

     

    Tweaking Yii

    Performance of web applications is affected by many factors, like db access, I/O operations, network bandwidth, 3rd party services…etc
    On high traffic applications, many tweaks can be done that enhance the performance of the application.

    Talking about tweaks/tricks in details will take us long time, so today we are going to talk in high level about concept and techniques. Hopefully in next articles we discuss each tweak in more details, and provide show cases before/after the tweak.

    1. Enabling APC Extension:
    PHP APC extension caches and optimizes PHP intermediate code, and avoids the time spent in parsing PHP scripts for every incoming request.

    2. Disabling Debug Mode:
    Many Yii components have too much logging statements that run in debug mode, so disabling debug mode will stop logging non-necessary code snippets.
    Note: Debug mode is disabled by having the YII_DEBUG set to false.

    3. Using yiilite.php:
    By default Yii bootstrap file is yii.php, which is responsible for bootstrapping Yii, and including common libraries and scripts. On the other hand, on each release Yii provides a file named yiilite.php which is a clean/comments free version of the yii.php file, plus with a reduced number of script includes, since common libraries already merged to one file.

    4. Caching:
    Frequently accessed data should be cached, such as db schema, complex view, models …etc

    5. Database Optimization:
    It’s not a secret that Active Records degrades performance on both db level and parsing level, due to the fact that each active record instance represents a connection to the db, and can run heavily queries to db, and might append unneeded conditions to the query specially on count queries.

    Active Records is good for modeling data in an OOP fashion, but on the other hand degrades the performance of application.

    Hence it is preferred to have heavily queries, queries that deals with enormous data, to be run using low-level database APIs instead of using active records.

    Queries should avoid unneeded joins, and try to lazy loading related models instead of eager loading.

    SELECT queries should have LIMIT statement to avoid exhausting the memory allocated to PHP.

    Tables with enormous data should be indexed.

    It is recommended to use database views for complex queries instead of issuing the queries from PHP and passing it to DBMS to parse them.

    6. Minimizing Script Files:
    External Javascript/CSS files should be minimized and combined, to avoid too many requests from each client, and hence reduce the network transmission time.

    7. Symlinking assets:
    Asset manager after doing include/minimize/combine process copies generated files from their original path to assets directory, this process can be tweaked by stopping the copy process and replace it with a symbolic link to the generated file.

    Conclusion 

    • Yii is one of the best MVC frameworks that can be used to develop web applications.
    • Yii is lightweight, efficient, and feature-rich framework.
    • Yii intensively follows OOP standards.
    • Too many tricks/tweaks can be followed that increases application performance.
    • Controllers should be thin, models should be fat, views should avoid too much logic, and/or dealing directly with model logic.

AABU GTUG Opening Event – February 17, 2011

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 introducing the AABU GTUG, and assuring for cooperate working with professors and students to increase the awareness of students about emergent technologies. On the other hand, we’ve announced to our first initiatives to students which is “Made in AABU”. Finally, read the schedule of our event and introducing the officers to students who are “Ahmad Hamad, Inas Shoman, Mohammad Jaradat, Mohammad Mashagbe, and Salam Al Sarayreh”.

Prof. Ismail Ababneh, participated in showing the support and help provided from college of I.T at AABU to the GTUG chapter, and motivating the students to participate and join our chapter.  Followed him Vice president of AABU Dr. Hashem Al Masaeed, and talked about technologies in general and its impression in our life, and mentioned number of Google’s technologies and its impression in our life, such as Google’s Earth, and Google’s Docs in addition to the biggest search engine in the world Google’s Search engine.

Eng. Mohammad Olaimat from his side introduced the Google web toolkit GWT to students and illustrated its architecture and approach in developing websites, and the main features in it.

Later on, our officers Mohammad Jaradat and Salam Al Sarayrah made a demo on HTML5, and introduced the main features in it. After that I (Anas) presented a short paragraph about App Engine, and its main features, and how to run “Hello World” application inside it.

From another side, Ahmad Hamad, Inas Shoman, and Mohammad Mashagbe presented a session to introduce Android and its main features and demos on it. Latest session presented by me (Anas) and was about Chrome Extensions, introducing it, and building a simple extension inside Chrome browser.

Finally, We’ve finished the session and thanked the attendees for their participation, and shared photos with each other.

P.S Here are some of the photos taken inside the event.

Cheers,

Anas Jaghoub,

Manager of AABU GTUG

 

View album View album View album View album View album
View album View album View album View album View album
View album View album View album View album View album
View album View album View album View album View album
View album View album View album View album View album
View album View album View album View album View album
View album View album View album View album View album
View album View album View album View album View album

 

G-Jordan Day-1 Event By Google on December 12, 2010

Today, Google run its first event G-Jordan in Jordan at Hayat Amman hotel, about 30 Googlers where there, presented sessions and talked about latest technologies from Google, they also talked about HTML5, Cloud Computing, Open web standards, Chrome Extensions. Attendances where professors from Jordan universities, and also students from different universities and countries like Lebanon, Palestine, Bahrain and more according to invitations sent by Google.

Really it was an amazing event, valuable, and successful.

Anas Jaghoub

P.S here some of photos from the event:

Nelson Mattos – VP, EMEA Product & Engineering

Sebastian Trzcinski-Clément – Developer / Industry Relations Lead, Middle East and North Africa

What you speech is what you see and what is Google translate :))

Chris DiBona – Open source and public sector programs manager and Me(Anas Jaghoub)

 

Luiselle Mozze a Googler and me (Anas Jaghoub)

Luiselle Mozze, Me (Anas Jaghoub), and my friends Mohammad Jaradat(left) and Ahmad Sharairi(right) of Luiselle Mozze.

Regards,

Cloud Computing Session at AABU–IT College on 2-DEC-2010

On 2-DEC-2010, I’d presented a session about “Cloud Computing” on the college of IT at Al-Al Bayt university, the session focused on introducing the concept of cloud computing, why cloud computing, area of usability, examples on cloud computing, advantages and limitations of cloud computing.

Attendances were students of I.T college in both departments Computer Science and Computer Information System, In addition to the head of department of Computer Science Dr. Sa’ad Bany Mohammad.

Here the link for the ppt slides of the session on Google Docs: Cloud Computing Session

another link on SkyDrive: Cloud Computing Session

Anas Jaghoub

P.S, Below Images from the session:

C# tutorial

Am glad to announce that there will be a C# .net tutorial to be started soon.
wish you all get benefits of this tutorial, for any questions, inquiries or feedback please don’t hesitate to tell us about it, remember you help us improve and spread knowledge for all.

Best Regards,
Anas Jaghoub

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