PHP is a framework

Rails, Django, Spring – application frameworks like these are now ubiquitous in the development of web-based applications. This is part of a trend in many general purpose languages of building toward the web. For these, the application framework is not just a buzzword; it is the abstraction used to build the language up to the browser.

PHP was designed with the reverse in mind. In its first release, it could have been considered to be a DSL for web-based applications. It was written to specifically address the needs to writing web applications, and is now building toward a more general purpose language. In all likelihood, this is the wrong way around. It certainly does not fall into the bottom-up design Shangri-La of which Paul Graham is such a proponent.

In reality, though, web-based applications are driving the industry. PHP is still very relevant in that domain. From very early on, PHP was centered on functionality that is considered basic to any web-based framework. Functionality without which each library is simply a thin wrapper over CGI. Features like easy access to GET and POST variables, session handling, header control, and database access have been built into PHP from, if not the beginning, then close enough that dinosaurs like myself don’t remember it ever being otherwise.

PHP has its own share of frameworks, too, which allow the programmer to painstakingly map SQL schema to XML or vice versa. They carefully abstract the logic out of the view and force the programmer to jump through hoops to write a simple application (although, for complex applications, this often makes the programmer’s life *much* easier.)

PHP does not need any of this. In fact, the most pragmatic framework I have used to date for PHP is the Simple PHP Framework, which is just a set of useful classes to automate some of the more tedious areas of web development. What it does not do is get in the programmer’s way. It is Occam’s razor at work in the world of web development. From the SPF website, quoting a non-existent link:

“All the web frameworks in the world won’t turn a shitty programmer into a good one.”

What PHP is not is elegant. It does not have beauty of prose, nor those features that make languages like ML and Lisp so exciting, such as closures, functions-as-objects, or lazy evaluation. Instead, PHP is, above all, a practical language. Instead, it has less sexy features, such as built-in support for XML, XSLT, and Xpath, support most databases, and the built-in ability to serialize to and from JSON and cookie strings.

It is easy to overlook how simple PHP makes many tasks that are complex in other frameworks. For example, Django’s form library is large and complex. I’ve no doubt that most other frameworks have equally Byzantine systems for developing reusable forms. In PHP, one might just write:

function input($type, $name, $display, $value)
{
    < ?
        

The function could be refined to separate the template and the code by putting the HTML into a separate include:

function input($type, $name, $dispaly, $value)
{
    include 'templates/input.inc.php';
}

Simple, right? Which is not to say that PHP’s faults are not legion. That said, I think that PHP sometimes does a better job of building toward general purpose programming than many other languages do of building toward web development.

Leave a comment | Trackback
May 15th, 2009 | Posted in Programming, Soap box
  1. May 15th, 2009 at 16:29 | #1

    I think that PHP was an original web framework. It was a way for Rasmus to do some web applications without having to write custom C based CGI. However, it seems to be more of a language and less of a framework now a days.

  2. May 16th, 2009 at 02:14 | #2

    PHP is awesome. While everyone is talking about this or that language, PHP just rocks you have everthing ready.

    PHP is not bad, it’s those who program in PHP who mess stuff up. But organize your code and you will see how beautiful it is to develop in PHP.

  3. Tony
    May 16th, 2009 at 07:18 | #3

    That’s why PHP is so popular, you don’t need a PHP framework to build a web application. With Ruby and Python, unfortunately, you need a framework.

  4. May 16th, 2009 at 07:38 | #4

    > With Ruby and Python, unfortunately, you need a framework.

    Not so unfortunately. They are general purpose languages. In those languages’ domains, PHP often shows its own shortcomings, like lack of threading.

  5. May 16th, 2009 at 10:35 | #5

    If PHP is a framework, it is now obsolete.

    While PHP is really simple for simple things, it is “unusable” for complex ones (not that you can not do almost “anything” in PHP, but the way you achieve it makes it unexplainable for newcomers and soon gets unmaintainable, therefore it is virtually unusable).

    While using one of the other “web frameworks”, no matter in what language a framework is developed, it will always be a love hate relationship between a language and a framework (i might exclude groovy because it is developed in parallel by the same people who build the framework (grails), so the developers would be biased towards introducing new language features, because of the needs of the framework).

    And back to “the problem” when using frameworks … you cannot trust frameworks. Frameworks evolve, and usually frameworks are not backward compatible and they are not maintained, usually, the sole pursue of framework developers is to develop new things and to do “old” things differently, “better”.

    This will most likely cause your application to break.

    If you trust your PHP skills, i strongly recommend not using any framework at all, but build your own stuff and reuse as much as you can. (i think more than half of PHP developers worldwide are developing in “solo” mode).

    In this way you will always “know” your code, it will always look clean for you.
    If the language/environment itself changes, you will most likely be able to adapt your old stuff (remember, i assumed you are an experienced developer).

    BUT, when you work in a team, with even more than 3 people, things will get fuzzy, unless you adapt a common denominator of style, USE A FRAMEWORK, and don’t always use what seems the easiest … a framework MUST have:

    - maintainability (how well maintained the framework is)
    - release cycles (how much time does it take from release A to release B of that framework or how often dramatic changes happen with the framework, that will break your existing apps)
    - bug fixing (are bugs being fixed between releases)

    You would never use a framework that is always in “bleeding edge mode”, unless its developers are real maniacs of code quality and just don’t care about releases and their numbers, which in reality, are just bloody numbers (django comes in mind)

    ONLY NOW:

    ONLY When the above are decided, and you have filtered about a dozen frameworks, you can start to look for features backed by the language and choose the one that BEST FITS YOUR NEEDS.

    I want to say that never choose the framework that “best fits your needs from the first place”. A team, will most like have a predefined “MUST HAVE” list of needs, only after those, one can decide on the “rest” of the needs.

  6. josh
    May 16th, 2009 at 15:06 | #6

    Your comparison to Django is a straw man. You can easily do the same thing in Django to manually write out form inputs. However, Django’s Form objects do far more than the seemingly equivalent PHP snippet you posted. Django allows you to generate the form object from your model object, which can be rendered to the page and read back in from the POST hash map directly to a model object and saved into the ORM, all in a few lines of code. It’s not what you would do for every single form, but you’re comparing apples to oranges.

  7. Jeff
    May 16th, 2009 at 15:43 | #7

    I don’t argue with that at all. In fact, what I was saying was that PHP has its own idiomatic method for dealing with forms that is quite high level and abstract that makes using a framework in PHP unnecessary.

    I think that Django is an excellent framework, and I have devoted quite a bit of time to replicating in PHP some of the needs that Django fulfills. My point was not whether PHP or Python/Ruby/Perl/Java + framework of choice is the one true Way. My argument is that PHP is itself a framework for writing web-based apps, and using a framework on top of it is an unnecessary abstraction that serves to drastically reduce the power of the language itself by forcing an idiom on the programmer that does not fit the language.

    I would further argue that SQL would not benefit from a “query framework” because it *is* the query framework; it is already a language for describing database queries.

  8. May 16th, 2009 at 16:32 | #8

    You should look at HTML::Mason in Perl – same functionality and much more. HTML::Mason is an OOP for templates.

    So, PHP is not a framework – it is half of a simplified framework – template language.