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 strongly 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.

Comments 5

  1. CJ wrote:

    > 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.

    Posted 21 May 2006 at 9:46 pm
  2. matt wrote:

    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.

    Posted 25 May 2006 at 12:43 pm
  3. bob dole wrote:

    you don’t like the semi-colon? the alternative sucks really bad.

    Posted 29 May 2006 at 11:57 pm
  4. someone wrote:

    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

    Posted 30 May 2006 at 3:41 pm
  5. yassen wrote:

    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.

    Posted 03 Jun 2006 at 7:33 am

Trackbacks & Pingbacks 2

  1. From Anne 2.0 » 2006 » May on 22 May 2006 at 4:47 pm

    [...] Peter Williams ยป Early impressions of PHP Peter, a Colorado geek, talks about some of PHP’s weirdities, like defaulting to passing objects by value instead of by reference. (tags: php programming) [...]

  2. From afongen » Sure it’s got warts, but it does the job. on 17 Aug 2006 at 10:35 pm

    [...] Peter Williams has started working with PHP. He comments mostly about the syntax and with the understanding that he’s writing about PHP 4. Some of that has been improved upon in PHP 5: exceptions, for instance, to which Matt Zandstra has written a good introduction. I agree with a lot of what Peter says. DHH has a point: PHP is not pretty to look at, and sometimes it’s ugly to use. Using -> as a method invocation operator is unpleasant (Perl does the same thing, but in Perl 6 it’s a .). It’s a small thing, but small things add up. I don’t like to use PHP because I like the syntax of the language or because it’s a joy to write PHP code. I like to use PHP because it gets the job done, sometimes quite powerfully. And it’s a helluva lot better in PHP 5. [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *