<ul><ul><li>PHP Developer Best Practices Burlington, VT PHP  UG </li></ul></ul><ul><ul><li>23 April 2008 </li></ul></ul>Ma...
About Me <ul><li>Software Architect, Zend Framework </li></ul><ul><ul><li>Development Lead: MVC, Zend_Form, Server compone...
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 ...
What we'll cover tonight Coding Standards
Purpose of Coding Standards <ul><li>Efficiency </li></ul><ul><li>Maintainability </li></ul><ul><li>Collaboration </li></ul>
Efficiency <ul><li>You know you don't have a standard when: </li></ul><ul><ul><li>You always need to reformat code origina...
Maintainability <ul><li>When following a coding standard, your code has a predictable layout both in the file system and i...
Collaboration When a team follows a coding standard, any individual developer can pick up another developer's code and und...
Coding Standards Provide... <ul><li>Consistency of  file system layout </li></ul><ul><li>Consistency of  code layout </li>...
Use Public Standards Don't invent your own standard for you or your company. You are not special, and your PHP source code...
Why use a public standard? <ul><li>Objectivity </li></ul><ul><ul><li>Choices are no longer based on subjective preferences...
What Choices Exist? <ul><li>PEAR Coding Standards* </li></ul><ul><li>Zend Framework Coding Standards </li></ul><ul><li>eZc...
What We'll Cover <ul><li>PEAR </li></ul><ul><ul><li>PEAR standards are the most widely used and accepted coding standards,...
PEAR Overview <ul><li>Popular component library </li></ul><ul><li>Issues have already been debated </li></ul><ul><li>Well ...
PEAR Naming Conventions <ul><li>Class Names </li></ul><ul><ul><li>MixedCase </li></ul></ul><ul><ul><li>Initial Cap </li></...
PEAR Naming Conventions <ul><li>Files </li></ul><ul><ul><li>1:1 correspondence between class name and file name; one class...
PEAR Naming Conventions <ul><li>Variables </li></ul><ul><ul><li>camelCased </li></ul></ul><ul><ul><li>initial character lo...
PEAR Naming Conventions <ul><li>Functions and Methods </li></ul><ul><ul><li>camelCased </li></ul></ul><ul><ul><li>Initial ...
PEAR Code Formatting <ul><li>Indentation </li></ul><ul><ul><li>Spaces only; no tabs </li></ul></ul><ul><ul><li>Four (4) sp...
PEAR  Block Formatting <ul><li>One True Brace </li></ul><ul><ul><li>Functions and Classes have the opening brace on the li...
PEAR Coding Standards <ul><li>More information </li></ul><ul><li>Current standards:  </li></ul><ul><ul><li>http://pear.php...
Zend Coding Standards <ul><li>Follow PEAR for the most part </li></ul><ul><li>Additions </li></ul><ul><ul><li>Omit closing...
Summary <ul><li>Use a  public  coding standard </li></ul><ul><li>Use it  always </li></ul>
Upcoming SlideShare
Loading in...5
×

PHP Developer Coding Standards

5,069

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
0 Comments
7 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
5,069
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
243
Comments
0
Likes
7
Embeds 0
No embeds

No notes for slide

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>http://pear.php.net/manual/en/standards.php </li></ul></ul><ul><li>Proposed standards for PEAR2: </li></ul><ul><ul><li>http://pear.php.net/manual/en/pear2cs.php </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>http://framework.zend.com/manual/en/coding-standard.html </li></ul></ul>
  23. 23. Summary <ul><li>Use a public coding standard </li></ul><ul><li>Use it always </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×