Sunday, September 3, 2017

PSR-1 and PSR-2 coding standards for PHP

Visual aspects of code play a significant role in raising or drowning developer's productivity. In case that there's too much clutter, no clear patterns and no lots of randomness in style, code reading can be difficult, and even stressful. This is why developers are constructing ways of dealing with the code complexity. They hope it can make everyday programming life easier. And I'd say they have the point. It really is important to have a clean, flowing, easy to read code.

One good example of such a coding standard is PSR-1, and it's extension PSR-2. Some of the rules of these standards are:


  • Files MUST use only <?php and <?= tags.
  • Files MUST use only UTF-8 without BOM for PHP code.
  • Files SHOULD either declare symbols (classes, functions, constants, etc.) or cause side-effects (e.g. generate output, change .ini settings, etc.) but SHOULD NOT do both.
  • Namespaces and classes MUST follow an "autoloading" PSR: [PSR-0, PSR-4].
  • Class names MUST be declared in StudlyCaps.
  • Class constants MUST be declared in all upper case with underscore separators.
  • Method names MUST be declared in camelCase.



More details on these coding standards can be found here and here.

PSR-1 and PSR-2 coding standards for PHP

Visual aspects of code play a significant role in raising or drowning developer's productivity. In case that there's too much clutte...