Coding standard for CakePHP

Coding standard makes you feel easy to maintain your code.Every coder knows their code very well.

But imagine you are not following any CAKEPHP or PEAR coding standard and you have written around 1000 lines of business logic. After 1 month client needs some modification  to the logic in that case it will be nightmare to read each line over night and fix the issue.

Lets focus on the real topic, following are the basic guidelines for cakephp coding standard

  • indentation by just 1 Tab
  • control structures (for, foreach, while, switch, if)
  • Always use full PHP tag inplace of short tag
  • funcation call
  • function definition
  • Naming convention
  • Variable Name
  • Constant variable

Indentation by just 1 Tab

if ($userType == 1){
   if ($userStatus == 'Y'){
      echo "You are the super user";

Always use 1 tab for each level of code. if you see above example first “if” condition is started from extreme lest and second “if” condition is started as just 1 tab from lest.

So for each level code, always use just 1 tab (press TAB button while writing the code).


Control structures (for, foreach, while, switch, if)

As per previous example, if you check the code clearly, there is 1 space between “if” condition and the starting “(”

First parenthesis “{” should be placed at the end of first line, this is applicable whether you are writing (if, for, foreach or while)  releated syntax in cakephp

Some of you may aware of ignoring “{” and “}” while writing single line of condition, but cakephp tells us, to use parenthesis every time, whether it requires or not.

following is a example of 1 wrong coding standard

if ($userType == 1) 
   echo "we are testing";

Function Call

Always try to provide default value in function parameters, suppose 1 function is accepting 2 parameters,

Wrong type function definition

function  getUser($dataType = null,$dataSection){
    $this->datauser =1;

Better way of function definition

function getUser($dataSection,$dataType = null){
   $this->datauser =1;

If you see above function call, you can understand that, here i want to prove that , 2nd function definition is better as compared to first function definition.2nd function is better, as second parameter having the default value null, so minimum it can take 1 parameter , but as per first function is concerned, it will take minimum 2 parameter, so it is not optimized.

Function Definition

Function name should be in Camel case, i.e. first letter of the function should be small.

function myFunctionName(){}

Variable and Constant Variable

Variable should be meaningful but we should keep it as short as possible.

first letter should be small, and camel case in case of multiple word.

Variable contains object, first letter should be capital

Constant variable name should be entirely capital. In case of multiple word, each word should be separated by the underscore.

$user = 1;
$userType = 2;
$ObjectData = new Dispatcher();

Keep Coding