PHP Coding Standard

Embed Size (px)

DESCRIPTION

This PPT covers Coding Standard followed by large PHP Community. It includes top best practices, do's and don'ts.

Citation preview

  • PHP Coding Standard (Best Practices)

    LavkushTripathi

  • 2

    Objectiveof the session

    1 What to write

    2 How to write

    3 Why to write

    To aware the standard (i.e. the best practices followed by the community or industry)

    PHP Coding

    Standard

    PHP Coding Standard (Best Practices)

  • 3

    Importance of the session

    For Developers For Managers

    Improve Code Readability

    Easy Debugging

    Less chance of bugs

    Documentation habit

    Improve Code Quality

    Easy Understanding for Trainee/new Joinee

    Easy Code Review

    Easy Quality Analysis

    Improved Bug Tracking and Source Code Revision Control

    Set up Organization Standard

    Quality Code Delivery

    Save Training Time

    PHP Coding Standard (Best Practices)

  • 4

    What we are going to learn today?

    1. Naming Conventions1.1 Class Names, Function Names1.2 Variable Names, Constant Names1.3 Single or Double Quotes

    2.Formatting2.1 Braces {} , Indentation Guideline2.2 Parenthesis () Guideline2.3 Control Structure Formatting

    3. Comments & Documentation3.1 PHP Comments3.2 File & Function Documentation3.3 SVN/GIT Commit Messages

    45. Sample PHP Codes

    PHP Coding Standard (Best Practices)

  • 5

    1. Naming Conventions

    1.1 Class Names, Function Names

    For Class Name:Use UpperCamelCasewithout underscores ('_')

    Avoid Abbreviations like HTML, PHP, CSS in class name

    Ex:class UserProfileController| class GetHtmlStatistic

    For Function Name:Use lowerCamelCasewithout underscores ('_')

    Ex: function actionListing() | function getUserList() | function isAuthUser()

    PHP Coding Standard (Best Practices)

  • 6

    1. Naming

    1.2 Variable Names, Constant Names

    For Variable Name:Use allLowercase with '_' as the word separator

    Common names for temporary variables are i, j, k, m, and n for integers; c, d, e for strings.

    For Private Variable Name: Start with (_) and use all Lowercasewords.

    Ex: $current_user| global $g_country_time_zone_lookup| Ex: private $_fields

    For Constant Name:Use allUppercase with '_' as the word separator

    Ex:

    PHP Coding Standard (Best Practices)

  • 7

    1. Naming

    1.3 Single or Double Quotes

    Ex: $count = 3;

    Ex: $search_keyword

    Use double quote when you want to replace a variable within a string with value.

    Ex: search_keyword

    PHP Coding Standard (Best Practices)

  • 8

    2. Formatting

    2.1 Braces {} , Indentation Guideline

    Use Pairing Braces (Start and End Braces from new line)

    Use 4 Spaces Indentation

    Don't Use Tab Indentation

    PHP Coding Standard (Best Practices)

  • 9

    2. Formatting (cont...)

    2.2 Parenthesis () Guideline

    Put a space after php keywords before opening parenthesis.

    Use parenthesis next to function names without giving space.

    Ex: return 1;

    PHP Coding Standard (Best Practices)

  • 10

    2. Formatting (cont...)

    PHP Coding Standard (Best Practices)

    2.3 Control Structure Formatting