Uly.me

cloud engineer

  • Home
  • About
  • Archives
Home/PHP/PSR-2 Coding Style Guide

June 15, 2015

PSR-2 Coding Style Guide

Laravel recently started using the PSR-2 style guide. PSR-2 is a coding style agreed upon by many PHP framework developers. It’s an extension of the original PSR-1, which basically has 3 main conventions.

  1. Classes are upper Camel case.
  2. Methods are lower Camel case.
  3. Constants are written in caps like VERSION_NUMBER.

PSR-2 expands this by using these standards

  1. Indents are 4 spaces instead of tabs.
  2. Keep line lengths at 80 characters. Soft limit of 120 characters.
  3. Use one blank line after a namespace declaration. Use a blank line after a group of declarations.
  4. Opening braces for classes MUST go on the next line. Closing brace must be on its own line.
  5. Opening braces for methods must go on the next line, and a separate line for the closing brace.
  6. Closures must have a space after the function keyword.
  7. Never use the var keyword, this is used with  JavaScript and would cause some confusion.
  8. Control structures, like IF, WHICH, FOR, and FOREACH must have one space before the condition parenthesis.
  9. The case keywords should be indented from the switch keyword.
  10. Keywords, like true, false, and null show be in lower case.

See examples.

Filed Under: PHP Tagged With: laravel, psr-2

About Me

I'm Ulysses, a Cloud Engineer at Cardinal Health based in Columbus, Ohio. I’m a certified AWS Solutions Architect. This website is my way of documenting the things I have learned in the Cloud. When off the grid, I enjoy riding my electric skateboard. I have surfed, snowboarded and played the saxophone in the past. I hope you will find this site helpful. It's powered by WordPress and hosted in AWS LightSail.

  • Cloud
  • Linux
  • Git

Copyright © 2012–2021