Early impressions of PHP

In my new job I am getting a chance to really learn PHP. PHP is the workhorse of the web these days. There are tons of web applications written in PHP, probably more than any other language or framework. This is primarily because it is so productive, as I found out a while ago. All that being said, I find PHP really weird. I should note that I have only used PHP 4. Because we use PHP 4 in production here, I have not take much time to check out what is different in PHP 5.

The first thing I missed was exceptions. Sure you can write software without exceptions but the result is just so ugly. Often you can barely see what is suppose to be happening because of all the isError() checks. Of course, skipping those makes things even worse because you end up with killing the PHP process because calling a undefined method is a fatal error.1

Perhaps the weirdest thing is the way PHP deals with objects, though. I have been working almost exclusively in stronglyagik”) OO languages for a long time now so I am very used to the parameter passing orthodoxy of “object reference by value”.2 In most mainstream languages you cannot pass objects directly as parameters at all. Rather, you always pass an object reference.

PHP, on the other hand, not only support passing objects directly but pass by value is the default way to pass an object. Unfortunately, that is almost3 never what you want, so object based code ends up with pass by reference indicators sprinkled all over the place.

And then there are the nits…

I despise explicit statement terminators, they clutter the code without adding any useful information. If you, as a language designer, really feel that you cannot take the time to write a parser smart enough to automatically detect the end of statements at least pick a termination character that is less ugly than the semicolon.

Most of the time I don’t seem much need in investing effort to reduce “finger typing” but the method invocation operator gets typed so much that making is easy to type really is worth the effort. It really should only be one character, and preferably one that does not require the shift key. PHP misses on both counts by making the method invocation operator ‘->’, which is two character one of which requires the shift key.

Still, even with all it’s issues there is no doubting what you can do with PHP if you are willing to get a little dirty.

{Update: Added note about only know PHP 4.}


  1. Dieing on an undefined method is amazingly inconvenient but probably the only real choice given the lack of exceptions.

  2. Passing the reference by value results in behavior that is quite similar to pass by reference but there are some subtle differences.

  3. I think I am being really generous here. I cannot think of even one situation in my entire career where I thought, “if only I could pass this object by value this code would be better.” But I suppose it is possible that pass-by-value might not always be wrong.

7 thoughts on “Early impressions of PHP

  1. > I have not take much time to check out what is different in PHP 5

    A rewrite of PHP’s object support was the major change in PHP5. Exceptions were added too.

  2. A likely rationale for always-pass-by-value is the stateless expectation of the script environment. Persisting references means lots of extra work that defaulting to state being completely marshallable “for free” as value-types may avoid.

    I’m not saying it’s a good reason, but scripting systems are, ultimately, problem-solvers purpose-built for their first application. The web, she is stateless…ish.

  3. http://www.php.net/releases.php

    Check the release date of php 4.0.0:
    * Released: 22 May 2000

    Not that its exactly an excuse for 4.0’s odd OO, but it might be an explanation. As a language, PHP has evolved from a perl module to a full fledged OO scripting language in only a few years.

    Already lots of things are ‘fixed’ in php5.x – it has real constructors, destructors, reflection, and exceptions – and php6 will continue the trend

    http://www.php.net/~derick/meeting-notes.html

  4. Ohh boys.. the so called “object orientation” of php4 killed me. If Rasmus has never did it (until v.5 at least), that would be better for me than being beaten as now and not knowing where do hits come from.

Comments are closed.