PHP Code Design: Standards and Rules
- January 11, 2018

Following the same rules simplifies the work of programmers. Making PHP code one standard helps in teamwork and raises the level of development.

Eight general rules

There are rules that are suitable for writing code in any programming language. Only following them will already improve the quality of your code.

  1. Make up understandable and readable names. Avoid Russian words in Latin transcription. Only English words denoting the essence.
  2. Indent each level and separate the logical blocks with an empty string.
  3. Reduce nesting and remove duplication.
  4. Control the length. We recommend for functions 20 lines or less, method’s less than 50 lines, classes less than 300 lines, files are less than 1000 lines. Limit the length of one line to the visible value on the screen. Soft restriction is 120 characters.
  5. Comment and document the code. This will fix all the necessary information.
  6. Use refactoring. Follow the principle of “refactoring, before and refactoring, more often.” We also recommend reading the book “Refactoring. Improving the draft existing code “by Martin Fowler.
  7. Work in the version control system. For example, Git as it’s free and convenient. Learning to work in it can be done in 11 sessions on the video course “Git. Fast start”.
  8. Learn the Open Source code. You can see how the leading developers write and take advantage of the best practices in programming.

Code Rules

At the end of 2017, PHP programming standards apply: PSR-2 and PSR-1. They set the rules for syntax, naming and design. All code must be written uniformly. This concerns spaces, indents, parentheses and lines.
In order do not remember all the requirements of the standards you can work in a modern development environment using PhpStorm, NetBeans and the like. They allow you to automatically format the text in accordance with the rules.

Indents

Properly designed PHP code assumes its simple visual perception. It is achieved by indenting and spaces. For indentation, use spaces instead of a tab character. Start each line with four spaces. The code should go to the ladder: open to the right, then reassemble.

Remember: one indent = four spaces.

We select indents of the body of the construction, the body of the method, import blocks, arguments and the like.
Spaces

Are put:

  • between for (foreach / while / catch) and (
  • after;
  • between) and {
  • before =>
  • after =>
  • between try and {
  • between} and catch
  • Do not put:

  • After the method name.
  • In the list of arguments before commas.
  • Between (and the name of a function or method.
  • Empty line
  • Inserted:

  • After each logical block.
  • After defining the namespace.
  • After the import block. Each line of the block must begin with use.
  • Round brackets
  • We do not stand for separate lines.

    Do not put spaces inside: after (and before).
    Put spaces before brackets and after.
    Inside, the enumeration is separated by spaces.
    Braces

  • An opening brace is placed on a new line in front of the method body, for classes.
  • The opening curly brace is not placed on a separate line in the constructions and closures.
  • The closing bracket} in constructs, method name, method definition, classes is written from a new line and separated by a single indent.
  • Arguments

    Are issued in two ways: in one line, separated by commas or in a column. Arguments on one line are written separated by commas inside the parentheses. Space is placed only after the comma.
    Comments in the code
    Clean code should be properly commented out. Unfortunately, there are two extremes: a detailed comment on each line and a complete absence of comments. Both that, and another hinders in work. Excessive commenting reduces the perception of code, distracts from an understanding of its essence. Write obvious things – spend your own and someone else’s time. Sometimes due to too detailed comments the amount of code is increased several times. Having finished with the code, look critically. Obvious and trivial comments delete.

    The reverse side of the coin is the absence of explanations. Codes with complex architecture, scripts, with a lot of nesting require notes. In this case, comments will help quickly navigate the code to other team members. Often they help the code developer himself. The main thing is to stick to the golden mean and comment only on the important points.

    Conclusion

    The code is not a personal littered box with a pile of papers. The correct code is a file in the Lenin Library. It all structured, documented important information, there are links to other directories and libraries. Net code can be parsed by both the pros and the beginner programmer. It is a pleasure to work with it and also describes the level of skill of the developer.
    Try to follow the accepted rules, recommendations and standards. Think about those who will work with him after you.

    recommended
    Smart Order Router. What is this?

    One of the key features of FinSafe’s trading platform is Smart Order Routing. A professional trading platform that a traditional …

    9 useful abbreviations in the JavaScript syntax

    Various shortcuts increase your productivity and make the JavaScript code shorter. And since short records are absolutely equivalent to long …

    Several reasons to explore Swift

    Swift is applied to create applications for Mac and iOS acting as a possible replacement for Objective-C. The latter, though …