Skip navigation.

The discipline of PHP

When people talk about PHP not working or not scaling for them, I sometimes think they are not talking about technology, but the fact that PHP is too easy for them.

A typical opinion on PHP will praise it for speed of development, but moan the fact that PHP coding is messy and horrible; for example, Ian Bicking says:

PHP has the right development methodology -- you start with some HTML, some forms and pages. You maybe factor out a few common parts -- header and footer, say. Then you add more logic in, treating the page like a script. Later in the project's life this seems a bit silly. In the PHP applications I've written, I always end up with a large portion of my files starting with with no HTML literals inbetween. It's also annoying to turn a PHP page into a reasonable function. But the basic concept is still good, even if PHP isn't a particularly good implementation of the idea.

Well PHP doesn't dictate a development methodology (unlike Zope or J2EE); so it's entirely up to the developer whether he wants to be sloppy, or methodical and precise (I don't mean to suggest Ian is sloppy - this is just an example). PHP has the same philosophy as C and C++, where the language is designed to be open-ended. PHP doesn't care how you structure your code or whether you choose good names for your variables or not. You could even use the names of your pets:

foreach (
      as $poopoo) 
Or unstructured obscenities directed at George Bush or Osama bin Laden:
                                          foreach ($worldLeader
 as $lyingPolitico) { Send(Them(To(Afghanistan($lyingPolitico))));}

In contrast, languages such as Ada or Modula are designed for a more rigid structured programming experience from day one. You can do this sort of structured programming in PHP too. It just requires self-discipline, and perhaps a team leader to enforce the standards.

Here's another example of lack of discipline causing good techniques to be discarded. While browsing through this slashdot link on PHP, i read the following:

Page-based approaches (PHP, JSP, etc.) don't really scale well. They are fine for a project consisting of a handful of "pages" but once you start dealing with 10, 20, 30 pages, the metaphor just crumbles and you need to start with a new type of design, like MVC. Unfortunately, I'm sort of cynical about the prospects of modeling interactive applications (face it, a lot of these web "sites" are really "applications") on a low level REST/HTTP protocol and think something new is needed like cURL. But in the real world, if you can refactor your application into MVC and use a page-based scripting (or templates ala Velocity, FreeMarker) for presentation only, that should scale better than pages. (i'm talking scaling in complexity, not load)

This is another misconception, equating more flexibility with less organisation or discipline. Although MVC is a legitimate design metaphor, there is nothing wrong with page-based approaches for even 100 pages. You just break them up into logical directories representing your needs. Each directory would typically have 1-10 pages, and code is shared using sensible includes. I bet you can find dozens of similar misconceptions on that slashdot page.

A common phenomenon of failed projects is to blame the tools. The programmer rarely blames him or herself for lack of initiative or discipline. For example, I conducted interviews with 2 Java programmers a few months ago. One complained that the Resin App Server they were using was very buggy and crashing, and they should have used Tomcat. The next day we interviewed the other programmer who said equally bad things about Tomcat, and suggested using Resin instead would have been more stable. We hired neither developer of course.

I'm sure this sort of blaming your tools happens in the PHP world too. As others have mentioned, PHP has low barriers to entry, and continues to be just as easy to use in PHP5 (despite the trolls who claim that PHP5 looks and feels like Java). A little bit more hygiene, a regular shave, and lots more discipline is the answer.

Comment viewing options

Select your preferred way to display the comments and click 'Save settings' to activate your changes.


cool site