What's the "right" PHP Framework?


Published on

This is a presentation that I recently gave at UpstatePHP in Greenville, SC evaluating the framework landscape in PHP.

Published in: Internet, Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

What's the "right" PHP Framework?

  1. 1. What’s the “right” PHP Framework? Barry Jones
  2. 2. Who am I? • Barry Jones • Java/Groovy/PHP/Perl developer since ’98 • Ruby on Rails developer mostly since 2012 • Formerly ran Brightball, Inc here in Greenville – Contract programming business from 2008-2012 – 99% CakePHP… – Built our platform “The Intersect” on top of Cake • Software Architect with ACS Technologies, Inc
  3. 3. SO WHICH ONE IS IT? The people need to know….
  4. 4. Is it… • Zend Framework? • CakePHP? • Symfony? • FuelPHP? • Code Igniter? • Lithium? • Laravel? • Solar? OR…. • Kohana? • Yii? • Prado? • Akelos? • PHP Fat-Free? • Agile Toolkit? • Silex? • Phunction? • K2F? • Phraw? • Qcodo? • Slim? • Phalcon? • Roll your own…?
  5. 5. WHY SO MANY? The million dollar question…
  6. 6. Before we answer that… Why do other languages seem to have clear leaders? • Ruby has Rails • Python has Django • Groovy has Grails • C# has MVC • Java has Spring
  7. 7. Many solutions to the same problem… usually indicates an underlying issue… which leads to lots of trade offs
  8. 8. What does that mean for PHP? PHP is bad at frameworks… …out of the box
  9. 9. So let’s dig into that a little • Ruby, Python, Groovy, C#, and Java have something in common • They boot up… – The application gets loaded into RAM – Runs initializer code – Makes its database connection(s) with a pool – Listens for requests and only processes that request – Can share RAM between requests – Built for long term garbage collection
  10. 10. PHP… • Reprocesses everything on every single request • Configuration • Route mappings • Database connections • Loading all of those framework files – Devastating to Disk I/O • Encapsulates and garbage collects each individual process
  11. 11. So a PHP framework has too… • Cache…heavily – And then reload those configuration caches on every request • Per request RAM allocation is HEAVY – It’s not loaded once and then processed – Framework is loaded every time for every request
  12. 12. A HISTORY LESSON Don’t worry, we’ll get back to the story
  13. 13. Rails lit the world on fire • Java’s Struts popularized MVC…but was awful • Rails made MVC good… – And EEEEEEVERYBODY copied it as best they could – CakePHP was a near clone for a while – Microsoft’s MVC framework…almost carbon copy – Grails…do I need to explain this?
  14. 14. But why did Rails take off? • Ruby is great…but it’s not built for the web • PHP is built for the web • Ruby is an excellent language for writing Domain Specific Languages (DSLs) – Think…. • Puppet, Chef, Capistrano, Vagrant – Rails is a DSL for the web • It changes Ruby to make it a web language • That added to its popularity significantly in the Ruby community – Nearly every Ruby library has a Rails version for premium integration – Ruby developers are very big on testing because of the language’s flexibility • good at testing and easy to break – Ruby developers place a premium on workflow • That’s a heck of a lot more than just arranging some files and throwing routing and hooks on top…they built an entire mode of operation
  15. 15. This is important for PHP PHP is built for the web out of the box With no framework… it can rock your face off
  16. 16. So why so many frameworks? • Well…prior to PHP 5.3 – PHP was terrible for frameworks for all the reasons previously mentioned • Also, lack of namespaces made sharing libraries a PAAAAIN – 5.3 was released in 2010 – CakePHP 1.1 was popular in 2006 because it ran on PHP 4 and PHP 5 while structurally copying Rails – The PHP “framework wars” started during a time when PHP was bad at frameworks…hence the variety, saturation, and trade offs • Everybody tried to solve the problem a different way
  17. 17. Prior to 5.3 you needed to… • Enable APC on the server – Optimized PHP byte code cached in RAM – Share RAM between process (under Apache mod_php only) – In production, set APC to not check for file changes – This got around the Disk I/O and overhead of reloading all those files – Also drastically reduced per-request RAM • With CakePHP we’d go from 12mb to 2mb (also mod_php only) • Configure a database connection pool within Apache – OR ensure you were using MySQL because the overhead of new connections is almost negligible • Avoid .htaccess files like the plague – Every framework used them for “pretty urls” instead of having them added to a server config • Setup a reverse proxy w/ nginx – Intercept non-application requests prior to Apache processing .htaccess rules • Use framework specific class loaders to try to load things only when necessary • Frameworks had to roll almost everything themselves to avoid naming conflicts with outside libraries – Extensive “does this work with X?” syndrome
  18. 18. So what changed? • In 5.3 – Late static bindings – Namespaces • In 5.4 – Traits – Artisan • Built in Web Server • CLI • These allow… – Lazy loading – Dependency injection – Aspect Oriented Programming (AOP) – Simpler development workflow – Much easier library / package management
  19. 19. What is PHP good at? • Scales down OR up better than any other language – “Boot up” web languages can’t scale down easily – With PHP, you can fill up a hard drive with code and it will all work • Other languages, you fill up your RAM and you’re done • This is why PHP shared hosting is everywhere and dirt cheap • Encapsulation of each request provides near perfect server stability – Tell me next time somebody uses “PHP” and “Memory Leak” in a sentence – If a request dies it can’t crash the server • In it’s raw form, it can already do almost everything you need – Example: Built an on-the-fly image processing server for a huge site – Same functionality previously kept crashing Rails processes …because memory leaks – Increased efficiency by 3000% (seriously)
  20. 20. Oh…and it can fly (the scaling up part)
  21. 21. What is PHP bad at? More on long polling • PHP is bad at long running process • Long polling involves many long running connections – which for PHP means many long running processes… – which will vary based on the number of your users So for example: • Optimal PHP request/response cycle – Request received – PHP started – Libraries loaded/RAM allocated (2-12mb) – Request processed – Request ended, garbage collected – Total time < .5 seconds (hopefully) • Long polling PHP request/response cycle – Request received – PHP started – Libraries loaded/RAM allocated (2-12mb) – ………. (30-60 seconds) – Request ended, garbage collected – Total time 30-60 seconds – Your servers RAM will be swallowed with a dramatically lower number of users from holding open dozens of 12mb connections – You can make it “work” but in production you’ll have massive scaling issues from concurrency • nginx_http_push_module – This is not a php specific solution – Request received with internal response address – PHP started – Libraries loaded/RAM allocated (2-12mb) – Requested added to queue – Request ended, garbage collected – Total time < .5 seconds – nginx holds the long polled connection instead of php – Single background process pulls requests off the queue and processes them – Sends response to the internal response address – The background process handles responding to the long polling request so you control how many are running in parallel, not your user traffic – nginx holds the concurrent connections efficiently (as good or greater concurrency than Node.js) Long running processes • Please don’t use PHP for long polling…ever • No really • All those documents that say PHP is bad at long polling…don’t take it as a challenge…they mean it • But if you must use nginx_http_push_module Also…method syntax consistency and assorted other quirks…
  22. 22. But it’s more than just code • The business problem: – How do I find programmers who know this framework? • Case: Brightball – I loved CakePHP. It was complex, had a steep learning curve but was incredibly powerful – We built a tool around it that would generate a permission controlled, data linked, interface based solely off of the database structure • AKA “The Intersect” (we were big fans of Chuck) • Really bad for billable hours  – Hard to find people who knew Cake (or any other specific framework) despite “popularity” numbers – With complexity, training and learning curve are bad
  23. 23. This is where Code Igniter took off • Code Igniter was not as awesome as Cake (sorry CI people) – I know one of you wants to argue this point…your opinion is wrong • But it was a heck of a lot simpler • Learning curve and training ease led to a huge following – And Expression Engine
  24. 24. Not a problem in other languages • Dominant frameworks lead to common knowledge bases among developers • Hard to find a Ruby programmer that doesn’t know Rails • That allows continuous evolution because businesses avoid the training/hiring quandry • Namespaces allows easy library integration and sharing between frameworks and for the language as a whole
  25. 25. Then there’s workflow… • Common frameworks lead to… – Common testing integrations – Common library integrations – Common database flow – Common team operations
  26. 26. So the answer is…? If I have to tell you the “right” framework... then there isn’t a clear choice
  27. 27. WAT!
  28. 28. No! Not that!
  29. 29. Just kidding 
  30. 30. LET’S TALK ABOUT LARAVEL This will be fun
  31. 31. First the framework wars • What about all the “other” frameworks – All of the existing dominant frameworks have to maintain a migration path for their user bases – This makes full reinvention on php >= 5.4…hard • Laravel does not have this problem – But it does need to gain widespread adoption
  32. 32. So first up… Laravel is a Rails clone And I mean that in a good way They didn’t just go for file structure Or callbacks Or active record Or middleware Or migrations Or testing Or workflow Or modularity They ate the whole…dang…thing
  33. 33. Libraries Ruby on Rails • Ruby has Gems • Rails applications have a Gemfile • Bundler installs Gems and manages dependencies PHP and Laravel • PHP has Composer – And PEAR – And PECL • Laravel uses Composer • Namespaces make this work
  34. 34. Dev Environment Ruby on Rails • Ruby has RVM – Ruby version manager – “cd” into a project directory and your ruby version and gemset changes • Bundled web server – From your project directory, your application runs – Dozens of projects in different versions, with different libraries and different dependencies are easy • Command line app access – Rails console PHP and Laravel • Php 5.4 + has Artisan – Going forward, running multiple local versions of PHP will be easier – Also will make tools like Foreman an option for complex apps • Laravel has Homestead – Complete Vagrant based dev environment – A framework that takes dev environments this seriously is huge – Will only improve thanks to Docker • Command line app access – Artisan CLI
  35. 35. Migrations Ruby on Rails • Migrations are “the” way • Necessary for teams working on the same code locally • Eases deployment across multiple environments PHP and Laravel • Migrations are “the” way • Virtually identical to Rails migrations
  36. 36. ORM “Laravel ships with a superb ORM: Eloquent. If you have used the Ruby on Rails framework, you will find Eloquent familiar, as it follows the ActiveRecord ORM style of database interaction.” – Laravel documentation
  37. 37. Deployment Ruby on Rails • You can always run your own… • But Heroku is where Rails apps begin • Deployment: – `git push heroku master` – Aaaaand that’s it PHP and Laravel • You can always run your own… • But Laravel Forge makes it easy to get  THAT On Linode, Digital Ocean, Rackspace or AWS
  38. 38. Other stuff Ruby on Rails • Built with testing in mind • Queuing is standardized • RESTful • Excellent localization • Extremely well documented • Rack Middleware layer • Highly modular • Very clean syntax Laravel • Built with testing in mind • Queuing is standardized • RESTful • Excellent localization • Extremely well documented • StackPHP Middleware Layer • Highly modular • Very clean syntax
  39. 39. What Laravel needs… • Like any PHP Framework, it needs saturation • It’s covered all the other bases – Powerful MVC framework – Developer workflow – Full language package management – Simple, fast deployment options – Very well documented – Built for teams (testing + migrations) – Modular • Just needs more people using it to solve the “hiring” case
  40. 40. Thanks! now go learn Laravel
  41. 41. Credits/Reference • http://www.techempower.com/benchmarks/#section=data -r9 • http://laravel.com/ • http://rubyonrails.org/ • http://cakephp.org/ • http://ellislab.com/codeigniter • https://www.google.com/imghp • http://rack.github.io/ • http://stackphp.com/ • http://php.net/releases/5_3_0.php • http://php.net/releases/5_4_0.php • http://www.php.net//manual/en/book.apc.php