PHP Developer Coding Standards


Published on

PHP Developer Coding Standards, presented to Burlington, VT PHP UG on 23 April 2008. First in a series of PHP Developer Best Practices presentations.

Published in: Technology

PHP Developer Coding Standards

  1. 1. <ul><ul><li>PHP Developer Best Practices Burlington, VT PHP UG </li></ul></ul><ul><ul><li>23 April 2008 </li></ul></ul>Matthew Weier O'Phinney Software Architect Zend Technologies, Ltd.
  2. 2. About Me <ul><li>Software Architect, Zend Framework </li></ul><ul><ul><li>Development Lead: MVC, Zend_Form, Server components </li></ul></ul><ul><ul><li>Scope Releases </li></ul></ul><ul><ul><li>Promote and Implement best practices for team and contributors </li></ul></ul><ul><li>Open Source Contributor </li></ul><ul><ul><li>PEAR </li></ul></ul><ul><ul><li>Solar </li></ul></ul><ul><ul><li>Cgiapp </li></ul></ul>
  3. 3. About You <ul><li>Can you read your own code? Can others? </li></ul><ul><li>Is your software tested? </li></ul><ul><li>Is your software documented? </li></ul><ul><li>Are you using source control? </li></ul><ul><li>Does your team work efficiently? </li></ul><ul><li>Do you push buggy software into production? </li></ul><ul><li>Do you have a deployment plan? Does it include contingencies for rollbacks? </li></ul>
  4. 4. What we'll cover tonight Coding Standards
  5. 5. Purpose of Coding Standards <ul><li>Efficiency </li></ul><ul><li>Maintainability </li></ul><ul><li>Collaboration </li></ul>
  6. 6. Efficiency <ul><li>You know you don't have a standard when: </li></ul><ul><ul><li>You always need to reformat code originating with others </li></ul></ul><ul><ul><li>Code you wrote 6 months ago has a different visual structure than code you write today </li></ul></ul><ul><ul><li>Whenever you modify old code, it breaks -- usu. due to newly introduced syntax errors </li></ul></ul><ul><ul><ul><li>e.g., missing braces in conditionals, missing commas in lists, etc. </li></ul></ul></ul>
  7. 7. Maintainability <ul><li>When following a coding standard, your code has a predictable layout both in the file system and in the layout. Easier to read code is easier to: </li></ul><ul><li>understand </li></ul><ul><li>maintain </li></ul><ul><li>refactor </li></ul>
  8. 8. Collaboration When a team follows a coding standard, any individual developer can pick up another developer's code and understand it, which will help them maintain and possibly refactor it. This leads to more efficient development... and thus happier customers.
  9. 9. Coding Standards Provide... <ul><li>Consistency of file system layout </li></ul><ul><li>Consistency of code layout </li></ul>
  10. 10. Use Public Standards Don't invent your own standard for you or your company. You are not special, and your PHP source code is not unique. Others have already argued the issues and you can benefit from their collective knowledge.
  11. 11. Why use a public standard? <ul><li>Objectivity </li></ul><ul><ul><li>Choices are no longer based on subjective preferences, but on empirical experience. </li></ul></ul><ul><li>Requirement when hiring or outsourcing </li></ul><ul><ul><li>Easily judge the candidate experience and code quality. </li></ul></ul><ul><li>Compatibility </li></ul><ul><ul><li>Use a standard that follows that of the libraries you use. </li></ul></ul>
  12. 12. What Choices Exist? <ul><li>PEAR Coding Standards* </li></ul><ul><li>Zend Framework Coding Standards </li></ul><ul><li>eZcomponents Coding Standards </li></ul><ul><li>PHPLIB </li></ul><ul><ul><li>* PEAR CS actually derives from Horde </li></ul></ul>
  13. 13. What We'll Cover <ul><li>PEAR </li></ul><ul><ul><li>PEAR standards are the most widely used and accepted coding standards, in large part due to the number of people using PEAR as well as the general quality and explicitness of the standard. </li></ul></ul><ul><li>Zend Framework </li></ul><ul><ul><li>Only to note the additions it makes to PEAR standards. </li></ul></ul>
  14. 14. PEAR Overview <ul><li>Popular component library </li></ul><ul><li>Issues have already been debated </li></ul><ul><li>Well known and accepted (used more than any other standard) </li></ul><ul><li>Basis for many other projects </li></ul><ul><ul><li>Horde (actually, based on Horde) </li></ul></ul><ul><ul><li>Solar </li></ul></ul><ul><ul><li>Zend Framework </li></ul></ul><ul><ul><li>AdoDB </li></ul></ul>
  15. 15. PEAR Naming Conventions <ul><li>Class Names </li></ul><ul><ul><li>MixedCase </li></ul></ul><ul><ul><li>Initial Cap </li></ul></ul><ul><ul><li>Underscores separate logical package and code boundaries </li></ul></ul><ul><li>Examples </li></ul><ul><ul><li>Spreadsheet_Excel_Writer </li></ul></ul><ul><ul><li>Services_Google_AdWords </li></ul></ul>
  16. 16. PEAR Naming Conventions <ul><li>Files </li></ul><ul><ul><li>1:1 correspondence between class name and file name; one class per file </li></ul></ul><ul><ul><li>'.php' suffix </li></ul></ul><ul><ul><li>Class name underscores become directory separators </li></ul></ul><ul><ul><ul><li>Spreadsheet_Excel_Writer </li></ul></ul></ul><ul><ul><ul><li>Spreadsheet/Excel/Writer.php </li></ul></ul></ul><ul><ul><li>No loose PHP code </li></ul></ul>
  17. 17. PEAR Naming Conventions <ul><li>Variables </li></ul><ul><ul><li>camelCased </li></ul></ul><ul><ul><li>initial character lowercase </li></ul></ul><ul><li>Constants </li></ul><ul><ul><li>ALL_CAPS </li></ul></ul><ul><ul><li>underscores for word separators </li></ul></ul><ul><li>Private and protected properties </li></ul><ul><ul><li>initial _underscore prefixes variable name </li></ul></ul>
  18. 18. PEAR Naming Conventions <ul><li>Functions and Methods </li></ul><ul><ul><li>camelCased </li></ul></ul><ul><ul><li>Initial character lowercase </li></ul></ul><ul><li>Private and protected methods </li></ul><ul><ul><li>Initial _underscore </li></ul></ul>
  19. 19. PEAR Code Formatting <ul><li>Indentation </li></ul><ul><ul><li>Spaces only; no tabs </li></ul></ul><ul><ul><li>Four (4) spaces per level of indentation </li></ul></ul><ul><ul><li>Purpose is consistency of viewing </li></ul></ul><ul><ul><li>looks the same regardless of editor </li></ul></ul><ul><li>Line length </li></ul><ul><ul><li>Suggested 75-85 character limit </li></ul></ul><ul><li>No shell comments (#) </li></ul>
  20. 20. PEAR Block Formatting <ul><li>One True Brace </li></ul><ul><ul><li>Functions and Classes have the opening brace on the line following the declaration, at the same indent level </li></ul></ul><ul><ul><li>Control structures keep the opening brace on the same line as the declaration </li></ul></ul><ul><li>All control structures use braces, no exceptions </li></ul>
  21. 21. PEAR Coding Standards <ul><li>More information </li></ul><ul><li>Current standards: </li></ul><ul><ul><li> </li></ul></ul><ul><li>Proposed standards for PEAR2: </li></ul><ul><ul><li> </li></ul></ul>
  22. 22. Zend Coding Standards <ul><li>Follow PEAR for the most part </li></ul><ul><li>Additions </li></ul><ul><ul><li>Omit closing ?> on any file containing only PHP code </li></ul></ul><ul><ul><li>Abstract and Interface classes </li></ul></ul><ul><ul><ul><li>Should contain the keyword in the class name </li></ul></ul></ul><ul><ul><ul><ul><li>Zend_Form_Decorator_Interface </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Zend_Form_Decorator_Abstract </li></ul></ul></ul></ul><ul><li>More information: </li></ul><ul><ul><li> </li></ul></ul>
  23. 23. Summary <ul><li>Use a public coding standard </li></ul><ul><li>Use it always </li></ul>