$30 off During Our Annual Pro Sale. View Details »

I can't believe you still do it that way: PHP best practices retrospective

I can't believe you still do it that way: PHP best practices retrospective

Slightly updated version of this deck for the SkiPHP opening keynote.

Laura Thomson

January 17, 2014
Tweet

More Decks by Laura Thomson

Other Decks in Technology

Transcript

  1. Laura Thomson
    @lxt
    I can’t believe you still do it that way:
    a best practices retrospective
    1

    View Slide

  2. Overview
    §What’s a best practice?
    §Architecture
    §Security
    §Scalability and performance
    §Development practices
    §Is PHP dead?
    2

    View Slide

  3. Format
    §Disclaimer: Anything with a black header is a vintage slide and
    may be dated
    §Reading your own old slides is like reading old code :(
    §Each topic through the ages 2001-2014
    3

    View Slide

  4. Best Practices
    4

    View Slide

  5. §Best is a strong word
    §YMMV
    §“Good practices”
    Best  Prac*ces
    5

    View Slide

  6. Your PHP should
    •be simple
    •get the job done
    •be secure
    •be scalable and performant
    •be producible and maintainable
    Principles
    6

    View Slide

  7. Architecture
    7

    View Slide

  8. •First, do the simplest thing that could possibly work.
    Rule  #1
    8

    View Slide

  9. Text
    Separation of concerns
    9

    View Slide

  10. 2001
    §Put reused templates in an include file
    §Avoid mixing php and html
    10

    View Slide

  11. 2004
    §Why are you still putting php in your html?
    11

    View Slide

  12. 2008
    §Why are you still putting php in your html?
    12

    View Slide

  13. 2014
    §I think we’re learning.
    §(At least, I hope so)
    13

    View Slide

  14. Frameworks
    14

    View Slide

  15. • PHP frameworks are proliferating, and Rails doesn’t help.
    • Having an architecture like MVC can be a really good thing, but:
    • Everybody has a different idea about how this ought to be
    implemented
    • Some of the ideas are really twisted
    • Some make it hard to do very basic things simply
    • Code bloat
    • Which framework?
    • No dominant paradigm yet, ergo little help with
    maintainability
    Have  a  clear,  simple,  architecture  that  is  easy  to  add  to,  easy  to  explain  to  new  developers,  and  easy  
    to  remember  now  or  in  two  or  five  years’  *me.
    Frameworks  and  Architectures:  use  and  abuse
    15

    View Slide

  16. 2001
    §What's a framework?
    16

    View Slide

  17. 2004
    §All frameworks suck
    17

    View Slide

  18. 2007
    §They still suck, mostly
    §Framework shaming starting to decline
    18

    View Slide

  19. 2014
    §PHP 5.4+ frameworks suck a lot less
    19

    View Slide

  20. Framework best practices
    §Lightweight beats heavyweight (unless it doesn’t)
    §CRUD, junior devs, MVP, giant teams
    §ORM is a kickstarter: training wheels for your app
    §Use what works, and throw it away when it doesn’t
    20

    View Slide

  21. More than the sum
    21

    View Slide

  22. Decomposition and decoupling
    #1 part of a good architecture
    22

    View Slide

  23. 2001
    §Functions
    §Include files
    23

    View Slide

  24. 2004
    §Objects
    24

    View Slide

  25. 2007
    §Reusable libraries
    §Services: everything’s an API
    25

    View Slide

  26. 2014
    §More libraries and components
    §More APIs
    §Better package/dependency injection tools
    26

    View Slide

  27. Robustness and
    Resilience
    27

    View Slide

  28. 2001
    §Write tests.
    28

    View Slide

  29. 2004
    §TDD
    §Use caching (disk, memcache) to hide service failures
    §Defensive coding
    29

    View Slide

  30. 2007
    §High Availability
    §Failover
    §Multimaster (sucks)
    30

    View Slide

  31. 2014
    §Minimize and isolate failure effects
    §Self-healing systems
    §Resilience: as much of it should work as possible
    §Feature flagging: turn off parts of your app that
    are broken or under load
    31

    View Slide

  32. Security
    32

    View Slide

  33. • Consider illegitimate uses of your application
    • Educate yourself
    • If nothing else, filter all external data
    –(From the PHP Security Guide at http://phpsec.org/
    projects/guide/)
    Basic  principles
    33

    View Slide

  34. • External data is not to be trusted.
    • What’s external data?
    • Anything from a form
    • Anything from $_GET, $_POST, $_REQUEST
    • Cookies
    • Some server variables (e.g. $_SERVER['SERVER_NAME'])
    • Database query results
    • Web services data
    • Files
    • The basic principle is to filter input and escape output
    • Filter input using whitelisting where possible
    • Escape output according to where it’s going.
    External  Data
    34

    View Slide

  35. • Some common problems:
    • register_globals issues
    • XSS (Cross Site Scripting)
    • SQL/Command Injection
    • Code injection
    • Other things to worry about:
    • Exposed source code
    • Session fixation
    • Session hijacking
    • Cross site request forgeries (CSRF)
    •Cross domain Ajax requests
    AGacks
    35

    View Slide

  36. What’s different?
    §register_globals and magic_quotes are dead.
    §Slides got less wordy.
    §Surprisingly little else.
    36

    View Slide

  37. Web app security
    37

    View Slide

  38. 2001
    §Use SSL
    §Don't store credit card numbers
    §Escape output naively (we didn’t know we were
    naive, honest)
    §addslashes()/stripslashes()
    38

    View Slide

  39. 2004
    §Filter input
    §Escape output.
    §Avoid XSS/injections
    §mysql_real_escape_string()
    39

    View Slide

  40. 2008
    §CSRF protection
    §Parameterized queries/prepared statements
    §ORM
    40

    View Slide

  41. 2014
    §Wider use of frameworks protects devs from
    mistakes
    §New developers learning the right way first
    41

    View Slide

  42. Scalability & Performance
    42

    View Slide

  43. • Profile early, profile often.
    • Dev-ops cooperation is essential.
    • Test on production data.
    • Track and trend.
    • Assumptions will burn you.
    General  Best  Prac*ces
    43

    View Slide

  44. •Decouple.
    •Cache.
    •Federate.
    •Replicate.
    •Avoid straining hard-to-scale resources.
    •Dev environment needs replication too.
    Scalability Best Practices
    44

    View Slide

  45. •Use a compiler cache.
    •Be mindful of using external data sources.
    •Avoid recursive or heavy looping code.
    •Don’t try to outsmart PHP.
    •Build with caching in mind.
    Performance
    45

    View Slide

  46. Principles
    46

    View Slide

  47. 2001
    §Don’t use CGI
    §Tune your queries
    47

    View Slide

  48. 2004
    §Cache everything
    §Query cache
    §Disk cache
    §memcached
    48

    View Slide

  49. 2007
    §Stand back, I’m going to use science
    §Measure, track, trend, profile, optimize
    §Build cachable code
    §Decouple reads from writes (write there, read
    here)
    §Replicate, federate
    49

    View Slide

  50. 2014
    §Refinement
    §Graph everything
    §Message queues to decouple writes
    §NoSQL/SQL: right data store for the job
    §DevOps
    50

    View Slide

  51. Coding
    Practices
    51

    View Slide

  52. •Above all, code needs to be readable and
    maintainable
    •Beware clever code:
    –cool design patterns
    –obscure performance tweaks
    –niche language features
    Write  good  code
    52

    View Slide

  53. •Turn error_reporting up in dev and
    display_errors off in production
    •Use set_error_handler() and
    set_exception_handler() for top level errors
    •Whacking all your code in a try…catch block
    is not a panacea.
    Errors  and  Excep*ons
    53

    View Slide

  54. •Have and use a coding standard
    •Zend Framework, PEAR, homebrew
    •Legacy code always a problem
    Coding  standards
    54

    View Slide

  55. Code through the ages
    55

    View Slide

  56. 2001
    §Write comments.
    §Make code reusable.
    56

    View Slide

  57. 2004
    §Have a coding standard.
    §Use OO!
    57

    View Slide

  58. 2008
    §Make it look as much like Java as possible.
    §Namespaces
    §Iterators
    58

    View Slide

  59. 2014
    §So many nice nice things
    §PHP 5.4 generators and anonymous functions and
    traits, oh my
    §(It’s beginning to look a lot like Christmas Ruby)
    §I’m kidding. Kind of.
    §Composer! PHPUnit! PHPDoc! Doctrine!
    59

    View Slide

  60. Process
    60

    View Slide

  61. • The process needs managing
    • Use a version control system
    • Have a testing and release process
    • Have an easy way for developers to set up a staging server
    • Plan for scale
                   Web  Dev  is  SoMware  Dev
    61

    View Slide

  62. Process through the ages
    62

    View Slide

  63. 2001
    §Test code on your own machine
    §FTP to production
    63

    View Slide

  64. 2004
    §Staging server
    §Deploy with ‘svn up’!
    64

    View Slide

  65. 2007
    §Actual TDD
    §Instrumentable code!
    §Repeatable process
    §Agile
    §Death of estimation
    65

    View Slide

  66. 2014
    §Better tools for everything
    §Continuous integration and deployment
    §Test automation and monitoring converge
    §Commoditization of version control
    §MTTR vs MTBF
    66

    View Slide

  67. PHP is Dead?!
    67

    View Slide

  68. PHP is not a best practice
    §PHP is an anti-pattern
    §Pinnacle of bad design
    §All PHP developers are incompetent
    §PHP is only suitable for making trivial personal sites
    68

    View Slide

  69. PHP is a toy language. You should use...
    §Perl
    §Cold Fusion
    §ASP
    2001
    69

    View Slide

  70. PHP is no good for enterprise. You should use...
    §ASP.NET
    §JSP
    §J-everything
    2004
    70

    View Slide

  71. PHP doesn’t have a single awesome framework.
    You should use...
    §Ruby on Rails
    2007
    71

    View Slide

  72. 2014
    PHP is for losers. You should use...
    §Python
    §Ruby
    §Node
    §Scala
    §Clojure
    §Erlang
    72

    View Slide

  73. 73

    View Slide

  74. PHP coders...
    gonna code
    74

    View Slide

  75. Questions?
    Laura Thomson
    [email protected]
    @lxt
    75

    View Slide