Links

pmuellr is Patrick Mueller, Senior Node Engineer at NodeSource.

other pmuellr thangs: home page, twitter, flickr, github

Tuesday, September 19, 2006

PHP is Doomed: a rebuttal

Imagine my horror, to take a new job in IBM doing some PHP stuff two weeks ago, only to find out last week that PHP is Doomed. Man, what crappy timing on my part, eh?

Impending Doom

I'm kidding of course; I don't think PHP is doomed. But the author of the article, Justin James, doesn't appear to be kidding to me; he seems quite earnest in his beliefs. So I figure I'd offer a rebuttal to some of his points.

PHP does not allow the programmer to multithread their application.

This point is brought up multiple times in the article. And of course, Justin is right. PHP doesn't support threads as a programming library capability like Java and other languages do. The reason Justin thinks this is the primary reason for PHP's demise is that you need multi-threading capability in servers; web servers now, when serving up pages, are going to be getting data from a variety of sources, and the only way to scale is to allow these separate pieces of data that make up a single page be retrieved simultaneously with multiple threads.

Justin uses Java as an example of a language that can do this multi-threaded processing on the server. Except, as it turns out, in Java Servlets, you are advised to not spawn your own threads. I won't go into the reasons, but found a message board entry here @ dWorks that provides the usual warnings: Re: Spawning Threads In Servlet. So, the fact is, you really shouldn't be spawning your own threads in a server.

Now, for the Java programmer, there's probably a way around this; your web container may provide extensions to allow you to spawn threads. For WebSphere, for example, you can use Async Beans. Or your container may support the WorkManager API. Or you may get some level of async processing through your middleware, such as asynchronously invoked web services. These bring additional complication, and in cases of container-provided extensions, may prevent you from writing completely portable code (portable across different web containers).

But there's still a problem. And it has to do with this thought (from the article).

If you are a Web developer, would you prefer your application to continue to process its work (for example, retrieving data from the local database, dynamically creating images, etc.) while waiting on the third party chunk of code (such as a Web service), and only have part of the page show a "Sorry!" message? Or do you think it is better for the entire page to take forever if the third party data source has a problem?

Threading is not simple. For example, how long are you willing to wait for that chunk to timeout, before using "Sorry!" as the content instead of what's supposed to be there? You're going to have to have a timeout. This implies that you actually need to synchronize on all the threads getting the individual chunks. Do you have a way to cancel a task that's taking too long? This is complicated stuff.

The obvious way around this, from my view, is to use AJAX to actually handle the parallel calls on the client. You've moved the problem from the server to the client, but it's safer to have it on the client anyway. And a user could, in theory, individually cancel pending requests, based on their ability to wait for them, instead of relying on presumably static timeout values in the server. But Justin doesn't appear to like the way of AJAX at all, which rules that option out for him. (Aside: he doesn't really explain why he doesn't like AJAX; I'd like to hear that argument).

Now, just thinking about moving your async tasks to the client brings up another point. JavaScript doesn't support threads when running in the browser; so how do they handle making HTTP requests in parallel? The model provided to the programmer is that of asynchronous programming with callbacks. When I make a call that would otherwise block, you pass in a function that the underlying code will call back on when the call succeeds or fails.

This style of programming is also available in the Twisted framework for Python (which I've never had a chance to use, personally; looks like fun though!). You can typically simulate threaded programming with asynchronous, event driven programming. With no threads. So, there's an answer for PHP straight off: they actually don't need threads, they need a port of Twisted. You of course need to have basic asynchronous i/o capabilities to build something like this, and it appears to me PHP already supports this for socket i/o.

Ruby is another language that Justin points out has thread support, but, in fact, it's probably not quite what he's thinking about. See Threads and Process from the online Programming Ruby book. Ruby's thread story is the same as VisualAge Smalltalk's: what we call "Green Threads"; they aren't operating system threads, but a threading system built into the virtual machine. As such, they will have limited capability to take advantage of multi-core machines, which is counter to part of Justin's argument that threaded applications can take direct advantage of multi-core machines.

There are things you could find to do with all those cores though; say ... virtualization.

Moving on ...

Its documentation is not very good, and frequently the comment threads provide the information that the documentation should have included but did not.

I would agree that the doc could use some work; I think a reorganized view of the existing doc might be enough to keep me basically happy; but doc quality is a constant issue (even with Java and Ruby). I actually like the end-user comment threads; someone tried this with Java a while back (@ JavaLobby?); wonder what happened to it? The one thing that I've not seen anyone compare with is the number of translations available for the base php docs: English, Brazilian Portuguese, Chinese (Simplified), Chinese (Hong Kong Cantonese), Chinese (Traditional), Czech, Danish, Dutch, Finnish, French, German, Greek, Hebrew, Hungarian, Italian, Japanese, Korean, Polish, Romanian, Russian, Slovak, Spanish, Swedish. Wow!

It lacks high quality tools such as IDEs and debuggers in a "ready-to-use" state.

Besides the well-regarded Zend Studio, if you enjoy the Eclipse lifestyle there are two PHP IDEs under development: PHP IDE Project and PHPeclipse.

I will give PHP this: it is easier to install on top of Apache or IIS than any J2EE server that I have encountered.

You sold me!


Update: fixed a broken link to the Creative Commons search page, linked to at the bottom.


Photo "Impending Doom", with a nice CC license, by Dr. Stephen Dann. Found using the Create Commons Search Site.

No comments: