2nd Impressions of PHP

I learned PHP years ago, as my first programming language. At the time I liked it, that is before I learned JavaScript and Python at my current job. Now I am back to PHP again because I've started writing Drupal modules.

My impressions are not great. Disclaimer: this is not a criticism of Drupal; I understand why Drupal is written in PHP, not Python (and of course not JavaScript). PHP:

  • Is probably the most popular server-side scripting language
  • PHP is on almost every host
  • PHP has been around for a while
  • PHP is (seemingly) easy to learn and write
  • There are a lot of PHP developers out there
  • etc. etc...

Drupal is great, but because it uses PHP, you are, of course, forced to live with PHP's weaknesses:

  • There's a function for everything. I count 97 standard string functions in PHP compared with 59 in Python and 16 in JavaScript. Sometimes it takes more time figuring out which function to use in PHP than it would take to write the function from scratch.

  • Incoherent and inconsistent function names. Example: strpbrk() searches a string for a set of characters. Huh? str_replace(), well you can guess what that does. Why is one function one word, and the other separated by an underscore? Why should I have to remember when to use an underscore? Programming is hard enough.

  • Built in functions are not instance methods. It gets real annoying to have to keep passing the object into the function:

    // PHP
    array_push( $my_array, 3 );
    
    // Python
    my_array.append( 3 );
    
    // JavaScript
    myArray.push( 3 );
    

    Python and JavaScript's syntax also makes the code much easier to read, which is in itself a huge plus in terms of maintenance.

  • Functions aren't first-class. In JavaScript or Python I can do something like this (in jQuery):

    // attaches a callback function to an element with an id of 'foo', when the element is clicked
    function myCallback() {
         alert('callback called');
    }
    $('#foo').click( myCallback );
    

    Notice that I am passing not the string name of the function, rather the function object itself. In PHP I would have to pass the string name of the function. This is a trivial example, but being able to pass functions to other functions allows a lot of flexibility and abstraction.

People have other and more sophisticated  gripes against PHP, I am sure. To be fair, PHP gets the job done, as does Drupal (if you have the patience to learn it). But sometimes I wish they would (or could) revamp the language entirely. Of course, then it may end up looking just like Python, and that's already been written.

© 2010 Your Name.. Drupal theme by Kiwi Themes.