Coding Best Practices

Edit on GitHub

This document outlines a few common PHP coding problems and the recommended solutions.

Merging arrays

When merging arrays, one usually uses array_merge($defaults, $options). However, when working with associative arrays (keys are all string identifiers), it is recommended to use the + operator. This is not only a lot faster, it also yields more correct results with edge cases. Beware of the switched order in this case: $mergedOptions = $options + $defaults;

Operations per line

To facilitate readability and debugging, it is recommended to use only one operation per line.

Method size

Long methods tend to have too many responsibilities, and are usually harder to understand and maintain than smaller ones. Therefore it is advisable to stick to the “single responsibility” principle, when a method is just a few lines long.