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.

    </li>

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

    </li>

  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.

    </li> </ol> </div>