You Always Play To Your Strengths, if PHP is your strength, that’s going to be your game
Getting Apache, MAMP, or Zend Server set up for local development is a chore. There is no getting around it. I develop on OS X and you have to edit the configs, install some packages, and more just to get a web server. This has changed with PHP 5.4 that includes a webserver, but there are still hurdles to get over
Installing Node.js is quite simple. Once you download and install you just tell node to run on the command line by
node server.js where
server.js is your main JS file.
Add to this, if it runs locally, it will run on Heroku
I was able to do some unit testing in Zend Framework, but it was nothing less than a fight. Getting PHPUnit installed was the first round of this fight. Then setting up your testing environment is the second and most grueling round. This is where PHP falls down flat, there are so many ways to do things, it’s hard to find a straight one solution to fit all.
Integration testing was not easy either, since I was using
Zend_Test_PHPUnit_ControllerTestCase I was always running into issues that made it difficult to get it working properly.
Thanks to TJ Holowaychuk, unit testing with MochaJS is pretty simple. I take that back, getting your head around it is a little of chore, but once you understand the difference between MochaJS (framework) and ChaiJS the actual tests, from there on out, it’s dead simple to unit test business logic.
Integration Testing is awesome. Get this, MochaJS employs SuperTest which uses SuperAgent all of these written by TJ. It actually fires up a Node.js server on a random port to run the test and then shutsdown. Once you have it set up, it’s really easy to write loads of API tests. Check out my dead simple Makefile… you just need to type
make test and it will run your tests (you can even have them run automagically when a file changes). Mind = Blown.
You’re not Facebook… yes Facebook uses a type of PHP and scales great, but again you’re not Facebook. Most likely, you’re hosting your PHP using Apache and
mod_php and haven’t really tweaked your
httpd.conf very much or done much with your
php.ini. If this is the case, there’s a strong chance that your app written in PHP will not scale to a second machine very elegantly.
Install app on Heroku, scale to infinity. This is how it works by default. Even if you didn’t use Heroku, it’s very easy to scale a Node.js app. This is because the app is pretty self contained. You could run many different Node.js instances behind a Nginx frontend.
The 12 Factor App lays out really good advice to web apps for ease of management, development and scalability. Let’s look at a few areas that many PHP apps by and large violate horribly.
PHP installs vary so much depending on which modules are install. I have never installed modules locally to satisfy the dependency. They are usually installed by PEAR in some common place on the server.
Node.js stores all needed modules locally in the app under the
node_modules folder. And NPM is the bomb (does that still mean cool?)
I have yet to see a PHP app that stores config in environment variables. It’s just not common practice at all. Take Zend Framework for example. It has the
application.inithat uses grouping to store variant configurations. This is NOT scalable.
Node.js encourages the use of environment variables and is required when using Heroku.
VII Port Binding
Again if you’re running PHP as an Apache
mod_php, you are most likely wondering how you can bind PHP to a port. I would suggest kicking
mod_phpto the curb and running PHP as a FastCGI process with PHP-NPM. With this set up, PHP runs as a separate process that listens on a port. In this case you can have several backend servers running your PHP code, and one Apache/Nginx server doing a round robin to these PHP servers.
Node.js Can bind to whatever port you specify.
This is not even a fair fight. PEAR is old and busted, NPM is the new hotness. I cringe when I have to try to use PEAR to install stuff. First you have to make sure you have a
channel installed in PEAR to get the packages that channel provides. Dependency hell is another reason to loath PEAR.
NPM on the other hand is so easy to use. It’s a central repo that anyone can upload modules to. All modules are stored in the local directory of that app so the app is entirely self contained. NPM also resolves dependency like a boss.
npm install express
Can you write complex, cool, fast, and awesome apps in PHP? Sure you can. Can you unit test, integration test, use environment variables, and satisfy local dependencies. Sure, but I’ve personally fallen prey to bad habits, forsaking unit testing, and more evils because it’s convoluted to get it done in PHP.
Yes: Good for your, you can appreciate the differences in different languages
No: I challenge you to write your next web app in something other than PHP (try Ruby on Rails, Django, Node.js, or gasp .NET). Try anything other than a PHP.
Did this help you out? It took me a few days to piece together all this information together, I hope this saves you some time (who knows, maybe the future me will be thankful I wrote this down). Let me know your thoughts. firstname.lastname@example.org