June 2, 2007
Update: now supports Ruby as well as PHP! I'll post a couple of Ruby examples next.
We're always getting grief from those PHP folks, aren't we? What if you could run PHP code directly inside ColdFusion?
<cfset who = "Sean" />
<cf_php>
<?php
echo "Hello ".$_COLDFUSION["who"]."<br />";
$_COLDFUSION["greeting"] = "wibble";
?>
</cf_php>
<cfoutput>greeting = #greeting#</cfoutput>
This outputs
Hello Sean and
greeting = wibble. Yes, you can pass ColdFusion variables into PHP and set variables in PHP that you can access back in ColdFusion land.
[Note: Unlike the first version I posted, the new version takes no attributes but instead makes available the entire variables scope from the calling page - and now uses $_COLDFUSION which is more in keeping with PHP.]
How is this possible? Well, ColdFusion 8 uses Java 6 which provides access to the javax.script package and all the J-language implementations available. Quercus is a Java implementation of PHP and it is implemented as a script engine.
Sun has provided a number of JSR-223 ScriptEngine and ScriptEngineFactory implementations and there are Java implementations of a number of languages.
Combine that with a simple custom tag and the code you see above becomes a trivial extension for ColdFusion 8!
Credit to axelclk for the missing piece of the puzzle!
Update: you can download what you need from my 'software' pod on my blog:
Scripting for ColdFusion 8. Installation instructions are given inside each script directory's
example.cfm page. The project now has Ruby support.
The source code is available on Google Code - along with my other Open Source projects and I'm setting up a central location on RIAForge as well (as soon as Ray approves my project request).
Comments
Rock n Roll !! This is awesome ! That opens up so many possibilities..
Posted By Rupesh Kumar / Posted At
6/2/07 5:11 AM
Wow this is great!!! Thanks for sharing!
Posted By Mike Huntington / Posted At
6/2/07 5:11 AM
That is pretty amazing! And did not take you long to knock it up either!
I have heaps of PHP books but never bothered to really read them as CF has suited me well for the past nearly 12 years :)
Posted By Peter Tilbrook / Posted At
6/2/07 5:19 AM
That's pretty cool Sean... Really cool.
Posted By Dean Harmon / Posted At
6/2/07 7:56 AM
Wow, very slick. I'd love to work with CF more for client projects but licensing seems to turn people off. Let's hope the new license helps with this.
Posted By christopher Cachor / Posted At
6/2/07 9:18 AM
So... real world. Here are my questions.
1. What does this give us? (What features does PHP have that CF is missing or would be enhanced by having?)
2. Does it just share the variable scope?
3. What are the chances of getting a shared hosting company to set this up?
4. If it is scripting, then it doesn't compile and cache, right? Wouldn't that be a performance hit? (Not to mention in order to get PHP to scale the creators have a paid for version of the free PHP, which isn't likely to work with this.)
NOTE: I think this is pretty cool. I just don't know the value of it in the real world yet. Much our company goal though is reaching beyond the enterprise so if anyone has serious answers to these questions let's hear them.
Posted By John Farrar / Posted At
6/2/07 9:53 AM
Hey, you should post this to RIAForge. :)
Posted By Raymond Camden / Posted At
6/2/07 9:57 AM
*THIS* is why its good to see you out of Adobe, you big brained fella you!
MD
Posted By Mark Drew / Posted At
6/2/07 10:41 AM
Well thats a tremendous stuff dude !!!! :)
Now serious, this is great stuff!
Thanks for sharing!
We, the new cf developers seem to ignore the java coupling of cf.
Now i am going to bash my .net friends also (cause they where always, look now vbscript, now jscript, no c#(C grill, not C sharp :P)
Posted By iongion / Posted At
6/2/07 11:02 AM
ACK! My eyes!
Seriously though, pretty cool Sean. Have you done any performance testing? Also, could you potentially wrap an entire PHP application in the tags ?
Posted By TJ Downes / Posted At
6/2/07 11:15 AM
So, like we've been saying "CF is Java!" to Java developers, we can now say "CF is PHP!" to PHP heads. ;)
Posted By Jake Munson / Posted At
6/2/07 12:04 PM
Jake, we can also say "CF is Python" to the .NET heads who are so proud of their python CLR implementations. :D
Posted By iongion / Posted At
6/2/07 12:28 PM
@John, good questions:
1. Imagine you have some PHP code you want to reuse in CF without rewriting it (see below for more possibilities)...
2. I have session scope shared in my latest p.o.c. (in PHP you say $_SESSION["varname"] and it's the same as session.varname in ColdFusion). I'm working on getting URL, form and CGI scope shared (trickier because of how the engine is wired in). PHP has no application scope.
3. If I switch over to Mark Mandel's Javaloader then you could use this on shared hosting I think.
4. The Quercus engine compiles the PHP to bytecode but I haven't figured out how to enable the necessary caching to make it only compile it once (yet!). Recommend you read the Quercus site for more details.
@Ray, yes, it was about after midnight when I first got it working and 2am by the time I gave up on URL / form scope integration so I wasn't awake enough to put it on RIAForge. It will end up there in due course.
@TJ, no performance testing yet - see my comment to John above. If I can get the rest of the integration working, you could potentially wrap an entire application, yes.
@All, the mechanism used to do this is fairly generic. Any language implemented as a javax.script engine can be used here. The next languages on my "hit list" to make work like this are: Ruby, Python, Groovy and Haskell. They all have javax.script implementations - see http://scripting.dev.java.net - I intend to wrap them all as custom tags for ColdFusion.
Posted By Sean Corfield / Posted At
6/2/07 12:32 PM
Sean, uses <cfset factory = thisTag.getClass().forName("com.caucho.quercus.script.QuercusScriptEngineFactory").newInstance() /> <cfset engine = factory.getScriptEngine() /> <cfset engine.getContext().setWriter( thisTag.getClass().forName("java.io.StringWriter").newInstance() ) />
so that serving with shared lodging, and createobject and cfobject blocked JAVA, he does not have problem.
Posted By Pedro Claudio / Posted At
6/2/07 8:03 PM
@Pedro, nice tip - thanx! Of course, on shared hosting you probably would not be allowed to install the scripting libraries in the first place...
Posted By Sean Corfield / Posted At
6/2/07 8:34 PM
Yes, it can be. But the server can have libs and also not allow createobject/cfobject JAVA. http://pcsilva.blogspot.com/2007/06/coldfusion-8-servindo-paginas-php-sim.html
Posted By Pedro Claudio / Posted At
6/2/07 9:42 PM
You're missing an underscore in one of your $_COLDFUSION vars.
Posted By Dan Russell / Posted At
6/4/07 9:43 AM
Posted By Sean Corfield / Posted At
6/4/07 9:59 AM
This is awesome. I have been wanting to port my site to cf for some time, but am tied into interacting with a php based bulletin board system. Im going to try this and get back you you guys with the results. Thanks for putting this out there Sean!
Posted By Josh Giese / Posted At
12/17/07 4:23 PM
@Josh, be warned that much of the context required for PHP includes and so on is not yet active in the scripting bridge I built. Simple sites run but I don't know how far you'll get with a complex PHP app!
Posted By Sean Corfield / Posted At
12/17/07 4:51 PM
Anyone know how to get this working on a shared server?
I am getting this error: Object Instantiation Exception. An exception occurred when instantiating a Java object. The class must not be an interface or an abstract class.
Posted By Jeff Pierson / Posted At
7/1/08 12:38 PM
@Jeff, given that it requires the addition of Java libraries into the core CF installation, I doubt any shared host will let you run this.
Posted By Sean Corfield / Posted At
7/1/08 12:47 PM
Hmm... now, if you can get it working to the point where you can run WordPress and phpBB through ColdFusion. No offense to the CF developers who've written blogging and forum tools for CF, but I don't think there's anything CF out there that competes with those. I've been kicking around the idea of installing PHP on our intranet servers because converting the whole site over to wordpress would be so much cheaper (right now the end users use Contribute) and so much easier to maintain.
But even as just a way to run some php code snippets inline, this is great. I don't know Java very well, so some of the more advanced text manipulation has always been a pain for me, where in PHP almost every kind of text manipulation you can imagine is already built in and easy to use.
Posted By JC / Posted At
7/14/08 6:42 PM
Hey JC, try out MangoBlog, http://www.mangoblog.org. It's not quite as mature as WordPress yet, but it's a really nice system
Posted By TJ Downes / Posted At
7/14/08 9:25 PM
@JC, and http://cf4em.com/ seems to be a good port of phpBB...
Posted By Sean Corfield / Posted At
7/14/08 10:30 PM
Post Your Comments
Hosting provided by