Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A modern approach to PHP development

A modern approach to PHP development

There have been some great advancements in the PHP ecosystem in the last couple years. Come learn about two tools that could make your projects more productive and enjoyable. The first is Composer, a modern dependency manager for PHP. The second is Silex, a small, lightweight framework that gets out of your way, but also gives you a great upgrade path as your needs change and grow over time. I will also discuss Vagrant, a great tool for provisioning a reliable development environment for you and your team.

Joel Clermont

August 12, 2013

More Decks by Joel Clermont

Other Decks in Programming


  1. A modern approach to PHP development Joel Clermont @jclermont Tuesday,

    August 13, 13 No bio slide. If you want to know more about me personally, we can discuss over a beer at Happy Hour tonight.
  2. Goals Tuesday, August 13, 13 I only have one hour.

    In order to be effective, we need to establish some clear goals up front.
  3. Goals • Expose you to some new ideas and tools

    Tuesday, August 13, 13 Not a dry “how to do X” technical discussion. There will be some “how”, but focus on the “why”
  4. Goals • Expose you to some new ideas and tools

    • Show you how they solved problems for me Tuesday, August 13, 13 Odds are, you have faced (or are facing) some of the same problems. No solution is “one size fits all” - so take the knowledge and apply it in your specific project, team and organization
  5. Goals • Expose you to some new ideas and tools

    • Show you how they solved problems for me • Get you interested enough to try it out later Tuesday, August 13, 13 I believe that the topics we cover today can make you more efficient, produce higher quality code and make your development more FUN. To me, less frustration == more fun.
  6. What do I mean by modern? Tuesday, August 13, 13

    Simplest definition: something newer than what you are currently doing. (tool, library, technique, language feature, etc)
  7. ¬ (Newer 㱺 Better) Tuesday, August 13, 13 Translation: Newer

    isn’t always better. (I love Unicode characters) The Art of Computer Programming is a classic example. These books are decades old but still extremely valuable. Developers tend to like shiny things. We often jump on new tools and technologies But newer isn’t always better. Remember previous disclaimer: I found these newer techniques better in my experience. YMMV
  8. Three talks for the price of one!! Tuesday, August 13,

    13 Structure will be 3 topics under one central theme I will pause for questions at the end of each section, but don’t wait. Interrupt me at any time (clarification, disagreement, etc) Interaction makes the presentation more enjoyable for me and more valuable for you.
  9. PROBLEM Wasting time setting up or maintaining a local dev

    environment Tuesday, August 13, 13 Option 1: NO local env. Edit and upload. Test on server (ouch) Option 2: OS X, use system apache/php/mysql (issues upgrading, configuring) Option 3: packaged env like MAMP. Pretty good, but leads to a second problem . . .
  10. PROBLEM “Works on my machine!” Tuesday, August 13, 13 Dev

    environment doesn’t match others on the team. Doesn’t match production Scenario: You’re running MAMP on OS X, everything works. Upload to production (Linux), getting 404 errors on some pages. Files exist. What happened?
  11. Solution Tuesday, August 13, 13 January 2010 Solves the issue

    of time/effort to setup and maintain your local environment Eliminates inconsistencies between team environments AND production environment Best of all, it doesn’t require you to force people to give up their favorite OS or text editor/IDE
  12. Uses virtual machines portable, isolated, project-specific Tuesday, August 13, 13

    runs on any major OS doesn’t mess with stock libraries one project can be Nginx with 5.5, other can be Apache with 5.3
  13. FREE Tuesday, August 13, 13 nominal cost if you want

    to use the VMWare infrastructure instead of VirtualBox all the tools I mention are free. Can’t beat the price.
  14. DEMO Tuesday, August 13, 13 maybe this all sounds a

    little too good to be true demo with Drupal (many OSS projects are distributing Vagrant configs for easy setup/testing) ~/vagrant/drupal
  15. Pick a “box” Tuesday, August 13, 13 Minimum configuration is

    picking a base box: usually a flavor of Linux Vagrant provides several common choices, Community provides many, many others You can even create your own
  16. Side note: disk space Tuesday, August 13, 13 You might

    be wondering about how much space all these VMs are going to take? Vagrant stores one copy of each base box Project has its own copy Most Linux VMs are 200 - 400 MB, not a huge impact
  17. vagrant  init Tuesday, August 13, 13 this creates your Vagrantfile

    really a Ruby file - don’t despair if you don’t know Ruby check it into source control
  18. vagrant  ssh Tuesday, August 13, 13 kick the tires, get

    full access to the VM, just like any server
  19. Oh no, do I have to use vim or emacs?

    Tuesday, August 13, 13 Remember, I said you didn’t have to give up your text editor or IDE of choice ssh is available, but there is a better way to edit files your project folder is sync’d into the VM at /vagrant anything you do in the host is sync’d to the VM if performance is an issue, NFS is an option (except on Windows)
  20. vagrant  destroy Tuesday, August 13, 13 Nothing is really lost

    when you do destroy. It will simply rebuild the box from your Vagrantfile Project code and data lives in your host OS
  21. How do I configure my VM? Tuesday, August 13, 13

    Remember, how I mentioned that your Vagrantfile is really a Ruby file? Vagrant has concept of provisioners: Chef / Puppet - learning curve Shell scripts: works, but can be a bit clunky and repetitive Ansible: declarative approach, but still has some learning curve
  22. PuPHPet == awesome Tuesday, August 13, 13 awkwardly named, but

    incredibly useful under the hood, it uses Puppet, but you configure it with a web UI if you want to learn Puppet, studying the manifests from PuPHPet is a great way to start
  23. References • Vagrant http://www.vagrantup.com • Drupal Vagrant box https://drupal.org/project/vagrant •

    Vagrant commands for the lazy and forgetful: http://codepoets.co.uk/ 2013/vagrant-commands-for-the-lazy-and-forgetful/ • PuPHPet https://puphpet.com Tuesday, August 13, 13
  24. PROBLEM Managing project dependencies is time-consuming and error prone Tuesday,

    August 13, 13 download a zip file? clone a git repo? PEAR PHAR files
  25. PROBLEM Vendor code is mixed in with your code Tuesday,

    August 13, 13 .gitignore files work, maybe git submodules. requires discipline
  26. Solution Tuesday, August 13, 13 Composer makes it easy to

    declare your dependencies per project AND to keep them up to date (this part can be the trickiest over time)
  27. Dependencies, not packages Tuesday, August 13, 13 Easy mistake to

    make, I even made it in my talk abstract packages are system-level, composer is project-level by default (can be used to install global packages) conceptually very similar to Ruby’s bundler or Node’s npm
  28. Easily specify a dependency level Tuesday, August 13, 13 what

    version of a library do you want? only master/production? dev ok? dev tools? it knows about your dependency’s dependencies (down the rabbit hole)
  29. How paranoid are you? $ curl -s https://getcomposer.org/installer | php

    Tuesday, August 13, 13 The installation instructions take curl output and pipe it into your php executable YOLO i recommend installing it globally in your path no need to manage the composer code per project
  30. composer.json  { "minimum-­‐stability":  "dev",   "require":  { "silex/silex":  "1.0.*", "doctrine/dbal":

     ">=2.2", "knplabs/console-­‐service-­‐provider":  ">=1.0.0", "igorw/config-­‐service-­‐provider":  "*.*.*" }          } Tuesday, August 13, 13
  31. composer  install Tuesday, August 13, 13 looks at composer.json and

    installs dependencies puts everything in a /vendor folder (easy to exclude from your git repo) creates a composer.lock file (snapshot of what was just installed) commit composer.json and composer.lock to repo if composer.lock is present, .json file is ignored and snapshot in .lock is installed
  32. Use your dependencies painlessly require  'vendor/autoload.php' Tuesday, August 13, 13

    install also builds an autoload file for you one single point of entry to all your composer managed code
  33. composer  update Tuesday, August 13, 13 follows rules in composer.json,

    but updates as far as possible while following rules sidebar on semantic versioning
  34. Tuesday, August 13, 13 the real power of composer no

    need to tell composer where to fetch each package 14,454 packages registered
  35. Is Packagist the only way? Tuesday, August 13, 13 you

    can specify a git (or other VCS) repo in composer.json if the package isn’t composer aware, you can autoload it if it supports PSR-0 if package isn’t PSR-0, use a classmap there is always a way
  36. composer  create-­‐project Tuesday, August 13, 13 Will do a demo

    later on Silex Basic way to install a framework, CMS, etc with one command
  37. Bonus points: create your own packages Tuesday, August 13, 13

    you can publish to Packagist host your own internal Packagist or lighter weight tool called Satis
  38. PROBLEM Ad-hoc project organization lacks structure Tuesday, August 13, 13

    300 PHP files in your public web root I’m sure you’ve never done that
  39. PROBLEM Lots of repeated or boilerplate code Tuesday, August 13,

    13 content negotiation: should I send HTML or JSON back? setting cache directives introspecting the HTTP request
  40. PROBLEM Automated testing is hard Tuesday, August 13, 13 500+

    line functions lots of side effects extremely high cyclomatic complexity (fancy way of saying there are too many paths through your code)
  41. Solution Tuesday, August 13, 13 MicroPHP Manifesto: January 2012 This

    could be an hour-long talk on its own, or even a full day workshop You aren’t going to learn how to use Silex, but why a micro framework is worth considering Silex is only one option: Slim is another great one. I’m sure there are others
  42. Why a framework? Tuesday, August 13, 13 It can enforce

    some structure on your project and make it a lot easier to avoid the big ball of mud You can still write complex, untestable, disorganized code with a framework.
  43. Why a micro framework? Tuesday, August 13, 13 Micro PHP

    manifesto Zend and Symfony (full stack) can be useful, but have steeper learning curve. Require higher team buy-in Micro-frameworks can fit almost any type of project I’m a fan of SOA, many single-focused services coordinating together YMMV
  44. DEMO Tuesday, August 13, 13 Common complaint: too hard to

    get started composer create-project fabpot/silex-skeleton silex-demo leverage both composer and vagrant
  45. Please TRY this on your own Tuesday, August 13, 13

    There will be some learning curve but remember the goals: more efficient, higher quality code, have more fun while developing
  46. August 11th – 13th 2014 Same Place, Same Time Tuesday,

    August 13, 13 Go tell all your fellow PHP devs about next year. I would like to see even more PHP topics and attendees next year. If you think of more questions, find me later or tweet me @jclermont. I’m here all three days