May 22, 2008

Things I miss in PHP

PHP has to be one of the most insulted languages of today. It's right on up there with Java (Visual Basic seems to be a language of yesterday, so it's not included here).

I must say, it's not so bad. From a management perspective, PHP is great. Hosting is plentiful and cheap; PHP programmers are easy to find.
From a server admin perspective, PHP is also great. It's easy to install, and the base install comes with pretty much everything the programmers will need (and then some). Compare this to Ruby, which has a mess of packages (called "gems") that need to be installed.
When PHP came out, it was arguably one of the best suited languages for what it does: making web pages dynamic. You can just embed the little things into the HTML! Wonderful! With Perl you had to put the stuff in cgi-bin and have it execute and output code, but PHP just fits right into the HTML that you already had. It integrates seamlessly with GET/POST requests and session/cookie variables. Database access is easy too, just go mysql_connect and you're set.

I feel though, that it is starting to show it's age. Ruby has some nice stuff now (like Rails) that make it able to compete at PHP's level. Embedded Ruby means you can embed Ruby code just like PHP into the HTML files. The MySQL module for Ruby means that it's just as easy to interact with the database as it is in basic PHP. I'm still not sure how you'd access the GET/POST/session/cookie stuff without Rails, I'll look into this one more.

What do I miss from other languages when using PHP? One of the biggest has to be function closures and anonymous functions. In Javascript or Ruby I can pass functions to functions. This can be done in PHP, but you have to declare the function with a name and then pass the name of the function to PHP (this brings headaches because you have to go function_exists() and pray to god that somewhere, somehow nobody named a function the same as what you want). I can't just pass the function itself. On top of that, functions can only see their local variables (unless you declare a variable as global). They can't access variables of an enclosing function. This may not sound like much, but there is many times when I feel an array_map or array_reduce would provide a much more elegant solution, but I can't properly define a function for doing this.

Nit-pickiness: Some nicer syntax would be good. Having to go $this->something whenever I want to access a member variable is a bit annoying (had a few bugs caused by this). Having to go array(...) to create an array makes some code harder to read, especially with nested arrays.
I wish the language was a bit more object oriented, so you could go ->method for basic types like arrays and strings instead of calling a function. Using array_merge(array_slice()... is somewhat difficult to follow sometimes.

All-in-all, PHP is not so bad. When you apply good software design techniques, there is no spaghetti code and the application is easy to understand. I've seen Java programs with hundreds of classes that feels more like spaghetti code than good PHP code (ok this method called this other method from a different class, let's go figure out what that method does, oh, that method calls 3 other methods of other classes like AbstractWhatTheFuckFactory, now I have to go find those classes and figure out what they do, but it turns out this other class was just an interface and now I need to figure out which class it actually was in order to figure out what's going on, etc...). I still get the feeling that it's trying to be more and more like Java when it appears that the community is moving away from the Java way of doing things.

4 comments:

Guillaume Theoret said...

Yeah that Java silliness of a huge number of classes and a huge number of methods (some people say methods should never be more than a few lines long and all classes should fit within an editor window without scrollbars which is ridiculous) is called ravioli code:

http://en.wiktionary.org/wiki/ravioli_code

Rob Britton said...

The next one will be called fettuccine (or macaroni) code...

I think that with every software methodology that comes out, there will always be people who take it too far.

Edward said...

I believe PHP is preparing the GET/POST/session/cookie stuff for you at the mod_php layer, doing the dirty work of parsing headers that you used to have to do yourself with cgi.

A good example of how to do this without Rails is chapter 18 (Ruby and the Web) of Programming Ruby (2nd Edition) by Dave Thomas.

[Stupid blogger... not letting me subscribe to the post after submitting my comment...]

Rob Britton said...

Nice, I'll have to take a peek at this one next time in Chapters.