What's the "right" PHP Framework?

41
What’s the “right” PHP Framework? Barry Jones

description

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

Transcript of What's the "right" PHP Framework?

Page 1: What's the "right" PHP Framework?

What’s the “right” PHP Framework?

Barry Jones

Page 2: What's the "right" PHP Framework?

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

Page 3: What's the "right" PHP Framework?

SO WHICH ONE IS IT?The people need to know….

Page 4: What's the "right" PHP Framework?

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…?

Page 5: What's the "right" PHP Framework?

WHY SO MANY?The million dollar question…

Page 6: What's the "right" PHP Framework?

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

Page 7: What's the "right" PHP Framework?

Many solutions to the same problem…

usually indicates an underlying issue…

which leads to lots of trade offs

Page 8: What's the "right" PHP Framework?

What does that mean for PHP?

PHP is bad at frameworks…

…out of the box

Page 9: What's the "right" PHP Framework?

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

Page 10: What's the "right" PHP Framework?

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

Page 11: What's the "right" PHP Framework?

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

Page 12: What's the "right" PHP Framework?

A HISTORY LESSONDon’t worry, we’ll get back to the story

Page 13: What's the "right" PHP Framework?

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?

Page 14: What's the "right" PHP Framework?

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

Page 15: What's the "right" PHP Framework?

This is important for PHPPHP is built for the web out of the box

With no framework…it can rock your face off

Page 16: What's the "right" PHP Framework?

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

Page 17: What's the "right" PHP Framework?

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

Page 18: What's the "right" PHP Framework?

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

Page 19: What's the "right" PHP Framework?

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)

Page 20: What's the "right" PHP Framework?

Oh…and it can fly (the scaling up part)

Page 21: What's the "right" PHP Framework?

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…

Page 22: What's the "right" PHP Framework?

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

Page 23: What's the "right" PHP Framework?

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

Page 24: What's the "right" PHP Framework?

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

Page 25: What's the "right" PHP Framework?

Then there’s workflow…

• Common frameworks lead to…– Common testing integrations– Common library integrations– Common database flow– Common team operations

Page 26: What's the "right" PHP Framework?

So the answer is…?

If I have to tell you the “right” framework...

then there isn’t a clear choice

Page 27: What's the "right" PHP Framework?

WAT!

Page 28: What's the "right" PHP Framework?

No!Not that!

Page 29: What's the "right" PHP Framework?

Just kidding

Page 30: What's the "right" PHP Framework?

LET’S TALK ABOUT LARAVELThis will be fun

Page 31: What's the "right" PHP Framework?

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

Page 32: What's the "right" PHP Framework?

So first up…Laravel is a Rails cloneAnd I mean that in a good way

They didn’t just go for file structureOr callbacksOr active recordOr middlewareOr migrationsOr testingOr workflowOr modularity

They ate the whole…dang…thing

Page 33: What's the "right" PHP Framework?

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

Page 34: What's the "right" PHP Framework?

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

Page 35: What's the "right" PHP Framework?

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

Page 36: What's the "right" PHP Framework?

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

Page 37: What's the "right" PHP Framework?

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 THATOn Linode, Digital Ocean, Rackspace or AWS

Page 38: What's the "right" PHP Framework?

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

Page 39: What's the "right" PHP Framework?

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

Page 40: What's the "right" PHP Framework?

Thanks!

now go learn Laravel