We're always getting grief from those PHP folks, aren't we? What if you could run PHP code directly inside ColdFusion?
<cf_php>
<?php
echo "Hello ".$_COLDFUSION["who"]."<br />";
$_COLDFUSION["greeting"] = "wibble";
?>
</cf_php>
<cfoutput>greeting = #greeting#</cfoutput>
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).
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 :)
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.
MD
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)
Seriously though, pretty cool Sean. Have you done any performance testing? Also, could you potentially wrap an entire PHP application in the tags ?
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.
so that serving with shared lodging, and createobject and cfobject blocked JAVA, he does not have problem.
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.
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.
Good move ma' boy many thanks!
Great post. Helped me setup and execute php using the ScriptEngine/javax.scripting interface.
However, PHP files that include other PHP files using include/require are not resolved. I keep getting "not a valid path". Anyone else run into the same issue?
Thanks.
<html> <head> </head> <body> <cf_php> <?php Echo "Hello, World!"; ?> </cf_php> </body> </html>
The error I get is ...
Variable APPLICATION is undefined. The error occurred in C:\ColdFusion8\CustomTags\php.cfm: line 30 28 : 29 : <!--- create Quercus engine and script cache: ---> 30 : <cfif not structKeyExists(application,"__scripting") or 31 : not structKeyExists(application.__scripting,"php") or 32 : not structKeyExists(application.__scripting.php,"cache") I think I installed correctly as per the sourceforge instructions. Any clues appreciated
I didn't see anything in the documentation about needing a specific application.cfc or particular code in an application.cfc , did I miss something?
If it's just "any old" application.cfc in order to have an application scope I'll slot one in tonight and retry.
Thanks for your reply.
the problem is that i can't pass variables from outside the cf_php to the inside...
I can't even grab the POST variable from inside the cf_php tag using php... This is a great idea, but completely worthless if it doesn't allow me to cross integration functionality....
http://scripting.riaforge.org/wiki/index.cfm/UsingPHPInColdFusion
Also, this is an open source project - please feel free to enhance it and submit patches back to me.
I don't do PHP so I've no personal need to enhance it myself right now. If I find myself doing more PHP, I probably will improve the integration.
<cfproperty name="projectID" displayName="Project ID" type="numeric" default="0" required="yes">
<cfscript>variables.lockName = CreateUUID();</cfscript>
<cffunction name="init" access="public" returntype="ModelDb" output="no" >
<cflock name="#variables.lockName#" throwontimeout="yes" timeout="10" type="exclusive">
Great thanks in advance
My problem is: Within the example.cfm, I had set a Coldfusion session called "uid" i.e. <cfset session.uid = "12345" />
I have another file test.php, and within that I want to access this session.uid and I tried to access it using $_SESSION["uid"] via PHP code, but it is not giving me the value.....
Please help me in resolving this problem.. Is it possible to access Coldfusion sessions(set in CFM) in PHP file...
I can access this session within the CFM files...But I want to access it in .PHP file...
Eagerly waiting for the response...
Thanks Nagesh.
I was trying to run php in CF and I found this blog, it was bery helpfully but I have some problems with a function wich calls a method named openssl_get_privatekey.
In php I just have to uncommnet the import tag where php_openssl.ddl is called in php.ini file, and after that everything works, but in CF can not do this. Any idea how to call the method openssl_get_privatekey?
Thanks.


