Feed Sign in with OpenID OpenID

Simon Willison’s Weblog

New PHP experiment, inspired by ColdFusion

I’ve been reading up on ColdFusion MX recently, and I have to admit it looks like a really nice piece of technology. I’d previously written ColdFusion off as being too simplistic and primitive, but having seen how much its capable of I’m reconsidering my position.

If you’ve never seen ColdFusion before, it’s a server side scripting language/application server from Macromedia (who obtained it when they bought Allaire). MX is the latest version of the language, which sees it completely rewritten in Java to allow it to integrate with Java application servers and existing Java servlet applications. This is shrewd marketing on the part of Macromedia, and I’ve already seen them advertising it as something to make your existing Java web deployments easier to customise.

The thing that put me off ColdFusion originally is that it is a tag-based scripting language, specifically designed to make it easy for HTML developers with little or no previous programming experience to pick up. Here’s an example of a chunk of CF syntax:


<cfinclude template="some-other-file.cfm">

<cfif isdefined("form.name")>
 <cfoutput>
Hi there, #form.name#
 </cfoutput>
</cfif>

The thing that most surprised me about ColdFusion is that although the above syntax looks pretty verbose, it is actually designed in a way that means you can do an awful lot with very little code. The above in PHP would look like this:


<?php
include('some-other-file.php');
if (isset($form['name'])) {
    print "Hi there, $form['name']";
}
?>

It’s less typing, but only by a bit. Where CF gets really clever though is with its handling of SQL. Here’s the code to run a SQL query and loop through outputting the results to a simple table:


<cfquery name="users" datasource="myDSN">
select name, email from users order by name
</cfquery>
<table>
<cfoutput query="users">
<tr>
 <td>#name#</td><td>#email#</td>
</tr>
</cfoutput>
</table>

Here’s the same in PHP, assuming $db contains a connection to a MySQL database.


<?php
$result = mysql_query("select name, email from users order by name", $db);
echo '<table>';
while ($row = mysql_fetch_assoc($result)) {
    echo "<tr>
  <td>$row['name']</td><td>{$row['email']}</td>
</tr>";
}
echo '</table>';
?>

The ColdFusion example achieves the same effect but with less complicated code.

ColdFusion is not without its problems: it comes with a price tag, and it encourages mixing application logic with presentation code (although as with PHP this can be avoided through discipline and careful application design). Never the less, I found it interesting enough that last night I spent a few hours putting together a PHP implementation of a couple of ColdFusion concepts. As with many of the experiments I post here this is very much experimental code—I won’t be supporting it and I would not recommend using it for anything more than casual experimentation. Anyway, here it is:

With a bit more development, something like this could be a useful tool for quick-and-dirty PHP scripts that simply pull data out of MySQL and display it as HTML. I still prefer PHP, but ColdFusion has a lot of good ideas which are well worth knowing about.

This is New PHP experiment, inspired by ColdFusion by Simon Willison, posted on 17th July 2003.

View blog reactions

Next: The Google Browser

Previous: Netscape R.I.P.

18 comments

  1. Simon: "ColdFusion is not without its problems: it comes with a price tag..."

    Bear in mind that NewAtlanta has decided to compete in the CFML space with their BlueDragon server. It isn't feature-comparable to Coldfusion MX (it's feature set is still one version back), but they're giving away a free, deployable version. It's missing some potentially important things (WDDX support), but there's enough there for many applications.

    I'd rather spend the money for Macromedia's version, personally, but since CF is part of my hosting plan, I don't have to worry about it one way or the other.

    Simon: "...it encourages mixing application logic with presentation code..."

    Not so much with CFMX. CFCs (Coldfusion Components) go a long way toward encouraging code and content to exist in different places. And as a bonus, exposing a CFC's data as a SOAP web service is as simple as adding an access="remote" attribute to the function definitions in a component.

    You'll also be able to do some cool stuff with XML-RPC very soon.

    Roger Benningfield - 17th July 2003 11:39 - #

  2. As a PHP person you probably havent looked much at ASP.Net, however it offers a similar templating system, with the use of code behind (where the code is kept in a seperate file) all that is listed on the HTML page is a couple of prefixed tags.

    Coupling this with the object orientation available in the .net framework and you have a very powerful set of tools to work with. You can define your own components and integrate them with your HTML while keeping all the application code in a compiled library.

    Ben Meadowcroft - 17th July 2003 12:08 - #

  3. I've actually read up quite a bit on ASP.NET, for the same reason that I've been reading about ColdFusion - the web problem is a tricky one so the more exposure to new ideas the better. .NET does look like a very good framework, although it's got some rough edges (the javascript: postback stuff is laughable, and it can generate some pretty horrible code if you're not careful). Never-the-less, it does have a lot of things going for it. I'm waiting for Mono to become stable before I start looking at it properly though.

    Simon Willison - 17th July 2003 12:35 - #

  4. Hi Simon, I wrote a longer reply earlier today, unfortunately I only got a frustrating error message, and the whole message was gone when I had to hit the back button ( I did not enter an email address). So instead of retyping it all I'll only mention that Harry Fuechs (from phppatterns) has done some work regarding .net-like templates in PHP. There's a thread over here: Parsing ASP.NET templates with PHP (sitepointforums). Maybe it will be of interest to some people. Also searching for "templating" on there will bring some interesting topics.

    On a sidenote: Maybe you can also do some fixing on the error message part on your commenting-part of the blogging-software (possibly redisplay the form with the errormessage; edit: I see that that's what you're usually doing. Somehow that's not what's happening when I leave the email-field blank). It's really frustrating, when the stuff disappears. Especially since this is the third this happened to me this month (not here, but overall). One persons blog would even swallow the message when hitting "preview", and return to the index-page... A tipp for all other bloggers: check your commenting-piece of your blog (i know I probably got to do it, too. I put it high on my priority list).

    Sencer - 17th July 2003 18:50 - #

  5. I've used CF in the past and, while I'm not a die-hard fan, many of the glaring weaknesses have been improved in the previous two versions. If you've dismissed it in the past (no user-defined functions in version 4.5!), it's definitely worth another look

    I think that, compared with other web development languages such as ASP and PHP, it wins hands down, on these points:

    • You want to get something up-and-running as quickly as possible. A huge amount of code is already written for you, ranging from easy SQL resultset iteration to form-field validation.
    • Your team isn't stacked with experienced developers, and/or you want non-developers to work with the files without breaking everything.
    • You don't want to mix-and-match software from a million different places. CF comes bundled with email, FTP, verity searching, scheduled tasks, web-based server administration, etc. Compare with out-of-the-box ASP, which lacks all of the above.

    Joe Grossberg - 17th July 2003 19:43 - #

  6. I have been working with CF for quite sometime and find it to actually much more easy to work with then PHP. CF teamed with the Fusebox methodology or MVC arcitechure with the new CF Components give you all the tools you need to create a nice application where your programming logic is separate from your display code. The best part of CF is how fast you can develop some pretty nice apps. Yes it does cost money but I have Macromedia's products to be excellent.

    Really when you get down to it PHP with a nice template and DB libraries can get your PHP development almost on par with CF. That is the combo I use when I need to produce an app for someone on a strict budget but who still wants a RAD app.

    BTW, thanks for the great weblog!

    Kurt Wiersma - 17th July 2003 20:12 - #

  7. Hello also JSP world is going towards a tag-style code... give a look at the new JSTL (Standard tag library) and JSP 2.0 specification

    JSP2.0: http://developer.java.sun.com/developer/technicalA rticles/javaserverpages/JSP20/

    JSTL: http://java.sun.com/products/jsp/jstl/index.html

    emi - 18th July 2003 09:15 - #

  8. PHP developers looking to expand their resume into coldfusion might also want to look into using the cfscript tag. It allows for a friendly scripting style interface in regular coldfusion templates.

    check this page, and the url mentioned at the bottom to read up on it.

    http://livedocs.macromedia.com/cfmxdocs/Developing _ColdFusion_MX_Applications_with_CFML/CFScript.jsp

    Nathan Strutz - 18th July 2003 17:44 - #

  9. I'm still stunned at the number of template libraries for PHP. However, I'm still leaning towards using PHP to generate XML and rendering to the browser with XSLT. In places where load and speed matter, then a proxy chain of Apache 2.0 --> Cocoon --> Apache 2.0+PHP may be best since you gain Cocoon's caching of XSLT through the XSLT to Java class compiler, but don't have to write Java. Meanwhile, my web host has moved me to a machine where with PHP has been built with Sablotron, so I can, if I want, ditch Smarty, produce Atom and render to any target through XSLT.

    Bill Humphries - 19th July 2003 04:01 - #

  10. Ben: quick question about ASP.Net, which I ought to take a look at.

    Is the model there a parser that looks for events pertaining to executable code and feeds the data in the element to a handler?

    In that case, is the right model to do things the ASP.Net way to use an XSLT engine that supports extensions aka EXSLT?

    Bill Humphries - 19th July 2003 04:10 - #

  11. "Im still stunned at the number of template libraries for PHP. However, Im still leaning towards using PHP to generate XML and rendering to the browser with XSLT. In places where load and speed matter, then a proxy chain of Apache 2.0 - Cocoon - Apache 2.0+PHP may be best since you gain Cocoon's caching of XSLT"

    Bill - you may want to check out Krysalis which is a port of Cocoon to PHP

    Harry Fuecks - 21st July 2003 10:46 - #

  12. I second the "take a look at modern JSP" comment. The SQL taglib in the JSTL makes quick-and-dirty SQL-to-Web interfaces trivial; and the core and XML JSTL taglibs are nifty for doing the presentation layer of larger-scale apps.

    Mike Kozlowski - 21st July 2003 15:10 - #

  13. Simon: Macromedia has released their Developers Resource Kit 4, which contains my XML-RPC Framework for CF.

    Roger Benningfield - 26th July 2003 00:03 - #

  14. Hi there! does your TemplateParser class support HTML entities? I'm developing a little template engine similar to Freemarker (developed in Java), based on SAX too, but when it finds an html entity i get parsing problems. Do you know how to solve this problem without using DOM? great work at your site, thanks and see you soon. ps: sorry if my english sounds a little strange :) sorry if my english sounds a little strange :) thanks and read you soon

    Vicente Reig - 12th August 2003 17:48 - #

  15. Hello there! I am a Coldfusion Developer for a few years now, working on a Windows Plattform. Has anybody got any information regarding ColdFusion and Linux (MySQL). I am mainly interested in a Replacement for Dreamweaver or Homesite on a Linux Platform. I only need the coding bit. I am planing to change over to Linux and try to get as much information regarding ColdFusion and Linux as possible. Hope any of you can give me some info. Thanks in advanced.

    John di Stefano - 5th December 2003 09:24 - #

  16. gula kha

    ramesh - 30th December 2003 19:01 - #

  17. I've been an avid fan of Coldfusion MX the past year, but upon getting into more advanced server scripts with regular expressions and web services, I have received more and more bugs using Coldfusion MX. Because of this, knowledge of PHP became valuable because every time I ran into these bugs, PHP was able to do it bug free. I now use Coldfusion MX up to its capabilities (I really prefer its security model and database features) because of its ease of use and fill in the gaps (most of the time very advanced stuff) with PHP (good support and widely used). Sometimes you have to compromise and bring in the best of both worlds. Everything good has something bad about it.

    rmunoz - 14th July 2004 07:38 - #

  18. New lines are not converted to breaks; use paragraph tags instead. XHTML must be well formed. The following tags are allowed: a, p, blockquote, ul, ol, li, dl, dt, dd, em, strong, dfn, code, q, samp, kbd, var, cite, abbr, acronym, sub, sup, br, pre

    ilghar - 12th February 2006 04:06 - #

Comments are closed.

Previously hosted at http://simon.incutio.com/archive/2003/07/17/phpAndColdFusion

A django site