When all you've got is PHP everything is a script

The title of the post is an adaptation of the suitable sentence "when all you've got is a hammer, everything is a nail".

I've been working now for 1.5+ years with PHP 5, and while I dislike it's ugly syntax and chaotic base functions lack of order (even in naming), I've also become aware of it's benefits (quick test and debug, fast for web tasks, flexible...).

But also I've detected an illness in many experienced PHP developers: They tend to forget that PHP is a web scripting language. Of course you can write command line PHP applications and use ugly (and multiplatform breaking) shell commands, but it is still a scripted language designed for the web.

Is not the complexity of developing a big scripted command line application (remember that if the script has no more code to execute, it will end, you cannot leave it on background and make it work asynchronously), is more related to the problems of trying to use PHP as a desktop/command-line/service/daemon source language:

  • A script cannot be constantly running in background. It has to be launched once and again each time, unlike a .NET Service or a Java application, in both of them being so easy to setup a timer that calls event handlers after periods of time.
  • A script can have a heavy/intensive "setup" phase. Having to initialize the same code once and again just with different data not only can present a scalability problem for the data storage system, but also exemplifies one benefit of having compiled languages/native applications.
  • Long-running scripts can have unexpected behaviours. Remember that PHP is created and optimized to serve web pages quick and fast, not to be left for hours in a single execution (i.e. operating with millions of items inside a loop). PHP can run out of memory, become sluggish or even hang.
  • Compiled languages will perform better, faster and more reliable: Whenever you go for C++, .NET or Java, any of them will be much faster and reliable. In C# for example is very easy to detect if you're getting out of memory or if the operating system is warning a service that it's going to restart.

I've seen big and heavy background job frameworks written in PHP, and they indeed work, but the have limitations, sometimes really severe.

Have you tried The Pirate Bay and used its awful search? Have you noticed that it only seems to work if you input full exact words? That is because it uses Sphinx, a PHP "full text search engine" that indeed is a search engine, but limited and poor compared with high-performance solutions like Lucene (which for example powers LinkedIn searches doing queries with up to 100 OR clauses in real-time).

It is logical to try to do everything with "the one language", but the same that you could but wouldn't buy a car made of wood, each language has its advantages and disadvantages. And specially a scripting language designed for the web is far from optimal to be used for desktop/server tools.


Posted by Kartones on 2010-09-29