PHP coding standards and naming convention for professionals


                           In my last article I shown you How to get element position using jquery ? Today I would like to write some basic coding standards which can be followed in your next project. “Remember your single line of code reflect your skills.” Coding standards vary developer to developers and company to company. It is really creating trouble for developers when we need to enhance the application or review the code which is not your own. But there are best practices you can follow to improve your coding style which I drafted recently.  

10 basic standards and Instructions to kick start your awesome project -


  • Development Environment –
It’s better to use always IDE for your project development, which will minimize your development time. Try to use Net Beans/ Eclipse IDE which is most preferable or optionally you can use subline, notepad++,geany etc. for code alignment.  

  • Project Root Directory Name –
Remember never keep your project root directory name in uppercase characters. It should be always in lowercase. Because it is looking ugly when directory name shows in the browser url.
Always keep your project root directory name similar to your project name which is more meaningful and professional.

  • Configurations –
Before starting your main business logic code always have your configurations files inside the single directory like configurations etc. its applicable for core php coder, MVC frameworks follows it’s own standards and location.

  • File Name –
Have your filename meaningful as it is reflect functionality of the page.
 For example- Never keep your filename un-meaningful name as like (user1.php, TheDefination2forlibrary.php, Payment.php etc.). It is better to have your file name also in lowercase but in some case like if you are creating libraries or classes you also can have your file name (Authentication.php, Validator.php etc.)

  • Doc Block –
Every pages or classes should have documentation block to describe the purpose and information about the page which is most important.

For example –
/**
 * Documentation Block
 *
 *
 * @controller name – PHP Coding Standards
 * @descriptions     -  You can have your detailed description here.,…
 *
 * @category           - Coding Standards and Structures
 * @author               - Sanjoy Dey - Software Engineer
 * @created date   -    29-07-2013
 * @modified by     - ..........
 * @modified date -
 * @todo                 - If any
 * @warnings        - If any
 *
 */ 

  • Class Name and Method Name –
Have single space indent from left side of your editor to start your class name. Have your class opening braces in new line and end up same.
Method name should start with single tab from left side of your editor and new line after your class opening braces. You should be more careful to have your method names, because in the case of MVC pattern it displays in your browser url. You can follow different style to have your awesome method names as below –

For example –

 class Employee extends Users
{

   /**
    * This is default method for Employee controller (Brief details about your method)
    * @access public
    * @param1 int default NULL
    * @return void
    */
     public function get_users($id = NULL)
    {
              ……………………….
    }
 
    private function _where_user($id)
   {
               ……………………….
  }

   private function getDetailsByID ($id)
  {
              ……………………….
  }            

}


  • ·         Conditional Statements –


            Start your opening braces of conditional statement always from same line, the main idea behind this is to differentiate between class, method and conditional statements.
Have a double space to start your statement from the method starting point.
In the case of long conditional statement you can have a enter and start next condition.

For example-

if($condition1){
   ……………………
}else{
  ……………………
}

if($condition1 ==true && $condition2 == ‘user’
   && $condition3 == ‘1’ && $condition4 == false){
      ………………………
}

Same follows for looping statement. Example of nested loops using this convention:

                   for ( $i = 0; $i < 5; $i++ )
                   {
                         for ( $j = 0; $j < 4; $j++ )
                        {
                              for ( $k = 0; $k < 3; $k++ )
                              {
                                     for ( $m = 0; $m < 2; $m++ )
                                     {
                                            ………………………………………
                                     }
                             }
                        }
                   }


  • Variable Name –
Variable name should be in lowercase and if you want to keep brief name it can be followed by underscore.
Define your variables before using to avoid warnings.


  • PHP Standard Requirement –
Recently while reading about the namespaces I came across the PSR standards. You can follow anyone of the PSR-0, PSR1, PSR-2 standard.


  • Before Deploying Your Code –
We always write some sort of code for testing and have it commented. Never forget to remove those commented or unnecessary code from your files before deploying into the server. Finally remove unnecessary backup files.


Follow the standards to become a better PHP developer -


A good developer always follows coding standards. It’s really up to you what kind of standards you follow. Well hope all these above instructions and standards will help you in your upcoming project. Leave a comment if above standards and instructions help you on joint project. 

All the Best.. 

You can also join my technical groups and stay updated, You can simply click on the follow us links below-  
Follow Us On Facebook Open Source Web Developers by Appsntech facebook group Twitter Open Source Web Developers by Appsntech twitter group Google+ Open Source Web Developers by Appsntech Google group Linkedin Open Source Web Developers by Appsntech, LinkedIn group
Copyright @2011-2015 appsntech.com. All rights reserved. Powered By- Sanjoy Dey Productions