Skip to content
alganet edited this page Feb 3, 2012 · 1 revision

Coding Standards

This is a work in progress. Things will change.

Loosely based on http://symfony.com/doc/2.0/contributing/code/standards.html and PEAR standards.

  1. Full <?php opening tags and no closing tag at all for class and standard PHP-only files.
  2. Echoing is allowed using <?= syntax.
  3. Indent by 4 spaces. Never use tabs.
  4. Single space after commas everywhere.
  5. No spaces after opening or before closing parenthesis.
  6. Single spaces around operators.
  7. Braces are forbidden for single-line statements. A line break after the one-line block is mandatory.
  8. Opening and closing braces for classes and methods are on a new line.
  9. Opening braces for control structures (if, while, etc) are on the same line, separated by a single space.
  10. PHP keywords are lowercase. (null, true, false, array, etc...)
  11. Constants are always uppercase with words separated by underscores.
  12. Follow the PSR-0 convention.
  13. Class structure follows this order:
  14. Constants alphabetically
  15. Static Properties alphabetically (public, protected, private).
  16. Properties alphabetically (public, protected, private).
  17. Abstract Methods alphabetically (public, protected, private).
  18. Static Methods alphabetically (public, protected, private).
  19. Methods alphabetically (public, protected, private).
  20. Name everything inside a class using camelCase.
  21. Name everything outside a class using underscore_separated.
  22. Avoid getters and setters.
  23. Use dockblocks only when needed.
  24. Getters and setters doesn't need dockblocks. Make them self-descriptive.
  25. Constructors and Destructors doesn't need dockblocks. Make its parameters self-descriptive.
  26. Private and protected methods doesn't need dockblocks. Make them descriptive or velociraptors will come for you.
  27. Type-hint everything you can.
  28. Untyped parameters must be considered "mixed" always and handle type casting if appropriate.
  29. Pass-only parameters must be preferable not altered.
  30. Use naming conventions for common operations:
  31. get() or getXXX()
  32. set() or setXXX()
  33. has() or hasXXX()
  34. all() or getXXXs()
  35. replace()
  36. remove() or removeXXX()
  37. clear() or clearXXX()
  38. isEmpty() or isEmptyXXX()
  39. add() or addXXX()
  40. register() or registerXXX()
  41. count() or countXXX()
  42. keys()
Clone this wiki locally