Archive for the 'PHP' Category

PHP !== Java

Saturday, December 20th, 2008

(This post originally appeared as part of the 2008 PHP Advent calendar)

Java has its place. Maybe it has many places. I’m OK with it being on your phone, in your refrigerator, and on my bank’s web site. I’m even OK with it making another play to be in browser applets. However, I’d really prefer if you kept it out of my PHP. I am seeing more and more PHP written as though it was Java with dollar signs. I think this is misguided.

PHP has its own pragmatic beauty. It does not try to check off enterprise buzzwords or be all things to all people. It does not try to be pure, driven down an organized roadmap, or marketed by a single owner. But it does get stuff done. Written well, it can be brief without being terse, and tidy without being restrictive. In its normal form, it will just work without significant planning and without consuming significant resources.

Using a spoken or computer language effectively is not just a matter of learning your language’s syntax and vocabulary. It is also about learning the quirks and idioms unique to that language, and using it in normal ways so that others don’t have to work to hard to understand you.

Translating the French Il pleut des cordes literally into English as It is raining ropes is nonsense. Literally translating the equivalent English Idiom It is raining cats and dogs into most other languages probably also results in nonsense. Potentially syntactically correct nonsense, potentially decipherable nonsense, but nothing that will flow naturally in conversation with a native speaker.

Translating Java literally into PHP gets similar results.

Here is a Java implementation of the singleton pattern:

public class Singleton
{
  private static Singleton myObject;

  private Singleton()
  {
     // Providing a constructor to eliminate default public one.
  }

  public static Singleton getInstance()
  {
    if (myObject == null) {
    myObject = new MyObject();
  }

    return myObject;
  }

  public Object clone() throws CloneNotSupportedException
  {
    throw new CloneNotSupportedException();
  }
}

In case you are not familiar with it, the singleton pattern is a general solution to many situations where you want only one instance of a particular class. Examples might include a database connection, a logger, or many user interface components.

Scatter a few dollar signs and make a few minor changes, and you get a valid PHP implementation:

class Singleton
{
  private static $myObject;

  private function Singleton()
  {
    // Providing a constructor to eliminate default public one.
  }

  public static function getInstance()
  {
    if (! isset(Singleton::$myObject) )
    {
      Singleton::$myObject = new MyObject();
    }

    return Singleton::$myObject;
  }

  private function __clone()
  {
  }
}

A native PHP speaker is much more likely to implement the same pattern as follows:

global $myObject;

if (!isset($myObject)) {
  $myObject = new MyObject();
}

Unfortunately, choosing this example might divert attention from the point I am trying to make. Global variables elicit a knee-jerk reaction in many people for a number of reasons, many of them good. In reality, more elaborate implementations of the singleton pattern deserve most of the same criticisms. Singletons are the object-oriented implementation of global variables.

PHP has always taken well-understood things from other languages. Why create new function names if people already know the names of the underlying C functions? Why not copy Java’s object system that many people already grasp? Why not offer Perl inventions that people find useful? But, the broth that this mix of ingredients creates has a unique flavor. It is not C, nor Java, nor Perl, and trying to program as if it is will probably leave you both missing the appeal of PHP and frustrated at the differences.

Like putting a hippo in a tutu, or a ballerina in a swamp, trying to force a language to be something it is not looks silly, works poorly, and gets leeches in your slippers.

OK, maybe I lied about the leeches.

You Used PHP to Write That?! - Uh yeah.

Monday, February 4th, 2008

I have gotten pretty good at staying out of language flame wars recently. Usually, when some wide eyed neophyte wipes a trickle of Kool-Aid from their chin and says “Ruby is sooooo much better than PHP”, I just say “uhuh”. I can’t be bothered opening a can of Terry Chay on them and I don’t really see the point. I can’t even usually be bothered saying “uhuh, that is why Twitter is the only A-list Web2.0 site built with Ruby and it is down all the time.”

Every now and again though, I just can’t help taking the bait.

You used PHP to write WHAT?! by Kenneth Hess on CIO.com grates. PHP programmers in general are pragmatic. It is not generally a language chosen by purists and zealots who latch onto one tool and claim it to be perfect in every way, and appropriate for every task. There are plenty of people with deep PHP knowledge who could have written an insightful article fitting into the CIO series’ theme that no one language is right for all applications.

Pap annoys me more when it appears in something claiming to be mainstream media than when it appears in some loser’s blog. Traditional media, while possibly doomed, does employ editors and generally attempts to check facts.

Well, sometimes they check facts. Ignoring the subjective parts that are merely the authors opinion, this article has so many simple factual inaccuracies that it is laughable. I assume the author does not have a great deal of experience with PHP.

Given he claims you can port an application from one database to another with minimal effort by running find and replace to swap mysql_query() with mssql_query() it seems likely that he has never written a non-trivial application in any language. So his major claim that PHP does not scale is presumably based on conversations he had with the fairies that live at the bottom of his garden.

Maybe it is just that Java fanboys push my buttons more than Ruby fanboys. Maybe it is just that damning with faint praise is more annoying than overt attacks. Barack Obama is “articulate“. Australia is “lovely” and reminds Bill Bryson of Iowa in 1958. PHP is good for “Creating an intranet site”. Come on!

Actually, it is might not not even be the factual inaccuracies and unsupported assertions that irritated me most. When I hear “enterprise” used as an adjective and not a punchline I involuntarily clench and it is there five times on one page.

Postscript: A couple of days later, Terry Chay could not resist opening a can of Terry Chay on them.

OSCON 2007 Talk: Striving for Less Ugly Charts and Graphs From PHP

Friday, July 27th, 2007

Here are the slides for my talk today.

Striving for Less Ugly Charts and Graphs From PHP

OSCON 2007 Tutorial: PHP and MySQL Best Practices

Wednesday, July 25th, 2007

Here are the slides for our talk today.

best_practices.pdf

If this site is slow, you can try http://www.laurathomson.com

I ♥ register_globals

Tuesday, March 13th, 2007

I am aware that there are some things so shocking that you are not supposed to say them in polite company “Hitler had some good ideas”, “Tori Spelling is really pretty” or “I think I look really good in a beret” are all ideas so confronting that they are best kept to yourself regardless of how strongly you believe them.

I have a similarly shocking sentiment that I feel I have to share.

I really like register_globals in PHP.

There, I’ve said it. I can go away and order my I register_globals shirt now.

I (heart) register_globals

Sure, choosing to mingle untrusted user data and internal variables is a bad idea. Sure, if you are too lazy to initialise important variables with a starting value it gives you one extra way to shoot yourself in the foot. Sure, polluting global scope with form variables is going to be a mess in a larger app.

There remains something to be said for simple, elegant, readable ways to shoot yourself in the foot. PHP, like any reasonably complete programming language provides a whole host of other ways, so removing one is not particularly useful.

I used to teach PHP to beginners as a first programming language. I have introduced a few thousand complete novices to programming via PHP.

With register_globals on, this example is a short step from the “Hello World!” example:

<?php
if($name)
{
 echo "Hello $name";
}
else
{
 echo
  '<form>
   Enter your name: <input type="text" name="name">
   <input type="submit">
  </form>';
}
?>

It flows nicely from a “Hello World!” example. It can introduce variables and control structure if you did not provide an even softer introduction to them. It can be turned into an example with a practical use without making the code more complex.

This version may not look very different to you:

<?php
if($_REQUEST['name'])
{
 echo "Hello {$_REQUEST['name']}";
}
else
{
 echo
  '<form>
   Enter your name: <input type="text" name="name">
   <input type="submit">
  </form>';
}
?>

To an experienced eye, the two versions are almost identical. The second requires a little more typing, but nothing to get excited over.

To a complete beginner though, the second is a couple of large leaps away from the first. To understand the second version, somebody has to understand arrays, and PHP string interpolation. Both of these are important topics that they will have to come to in their first few hours of programming, but without register_globals, they stand in the way of even the most trivial dynamic examples.

I miss being able to assume register_globals as default behaviour. It made the initial learning curve far less steep. It made little examples cleaner and more readable. Like most safety measures, it does not really protect people who are determined to get themselves into trouble anyway. People who don’t understand the reasons behind it just run extract() or some code of their own to pull incoming variables out anyway. The user submitted comments in the manual used to be full of sample code for doing exactly that.

Oh, but just a side note to all beret wearing white supremacist Tori spelling fans, just because I am willing to speak up for one unpopular cause does not mean I am interested in yours. Sorry.

Melbourne PHP Users’ Group - March 8th

Wednesday, March 7th, 2007

On Thursday, I will be speaking at PHP Melbourne. My talk is titled PHP Considered Harmful. In case you are wondering though, it does not mean I have had a falling out with PHP. I have spent 10 years talking about what’s great in PHP and I need to vent occasionally. Come along if you are nearby. If not, and I am not strung up by an angry mob, I might redo the talk in another hemisphere later in the year.

The other speaker is Chris Burgess on Building Secure Web Applications.

His blurb:

This presentation expands on a presentation given at the Open Source Developers’ Conference in December 2006 titled “Web Application Security - Tools, Techniques, Tips and Tricks”. I will explore some of the original material for those who were unable to attend, taking a look at the plethora of Open Source tools that can greatly assist developers and testers of web applications. In addition to this, I will discuss techniques that can be used to harden web applications.

Maybe it is time I got out of PHP

Wednesday, February 7th, 2007

It struck me at Kiwi Foo that PHP’s place in the world has changed. Ten years ago, it was a niche tool for solving a particular problem, the Web problem. Using it felt like an adventure, clients had to be talked into it, documentation was patchy, you might not know in advance if something was possible.

Today, a great many things are being approached as a subset of the Web problem, and PHP is everywhere. Using it is the safe, easy choice. You are almost certainly not going to be the first person to try to implement some weird functionality. You are almost certainly not going to be the company to test its ability to run large sites. You will know what you intend to do is possible before you start. PHP is on the slow downhill slope to that dusty, tweedy place where boring old programming languages go to be tended to by boring old programmers.

It will not be long before you are driving past an old people’s home near you and see a sign reading “Bingo Mondays, PHP Classes Tuesdays, Lawn Bowls Wednesdays”

The dilemma really, is where would I go?

Clearly I could not switch to Java. Now don’t get me wrong, Java is a fine language for some things, it is just that the main thing it is good for is keeping an army of people who don’t really enjoy programming but enjoy earning a decent living productively occupied and off the streets. It is not ideally suited for the Web, and it is not something I would use for fun.

Ruby is nice in many ways, and although Rails is not as nice, Ruby’s main problem is the user community. I don’t remember the PHP community when it was the same size being infected with the same degree of religious zeal and rampant fanboyism. Trying to advocate a particular piece of technology and clinging to a blind refusal to admit that your technology of choice has any flaws at all is not convincing.

Python syntax annoys me.

Perl has already descended further down the dust, dentures and bingo slope than PHP.

Some things though have not changed over the years. PHP may have its wrinkles, but it is still a great tool for pragmatic people to get a job done quickly and efficiently. That of course is the reason that I will be using PHP tomorrow, and presumably for a while longer too.

I Am Up There With Paris Hilton?

Wednesday, September 27th, 2006

I am not quite sure what to think of this birthday list.

Simon’s top five list of “Presents NOT To Give Your DAD On His 60th Birthday” is:

* Free one year’s subscription to FHM
* Paris Hilton’s New CD
* Stretchable pants
* PHP and MySQL Web Development (3rd Edition) (Developer’s Library) by Luke Welling and Laura Thomson (Paperback)
* That small blue pill…

What can I say?

OSCON Wrap Up

Wednesday, August 2nd, 2006

George Schlossnagle, Laura Thomson, Luke Welling, Theo Schlossnagle, Chris Shiflett signing books at OSCON06.  Photo by Mark Taber.

George Schlossnagle, Laura Thomson, Luke Welling, Theo Schlossnagle, Chris Shiflett signing books at OSCON06. Photo by Mark Taber.

OSCON is my favourite conference. I really like the way it brings people passionate about a whole range of things together. Sometimes of course, they choose to concentrate on their differences, but for the most part somebody who is interested in one technology is more likely than average to be interested in others, and likely to have a great deal in common. Contrary to popular opinion, PHP does not officially stand for “People Hate Perl“.

Remind me next year that at the end of every OSCON I always wish I had spent more time outside the PHP track.

Highlights included
Rasmus, demonstrating how his name became a verb (and profiling a PHP app with Valgrind).
Terry Chay’s ongoing struggles with Tourette’s syndrome.
Zak Greant’s lightning talk on how PHP is saving the world a variety of unusual ways. (Hopefully he will write it up as a blog post or similar)
Cal EvansPHP’s Most Wanted cards, which you can download if you want your own set.

Measuring Open Source Popularity

Thursday, July 27th, 2006

Here are the slides from my OSCON talk today

measuring open source popularity.pdf