Monday, January 12, 2009

PHP/Groovy Bridge

Ah, something new and shiny in Project Zero to talk about.

Zero supports three languages: Java, PHP and Groovy. It also supports a workflow runtime called Assemble and since that stores its flows as XML, and workflows are kind of like code, I suppose it could be considered an unofficial fourth language. 

Anyway I digress. Up until recently the PHP runtime was treating Groovy code just the same as Java. Anyone could compile a Groovy class to Java class files using the Groovy compiler. Those class files could then be dropped into a Zero application and used in PHP through the PHP/Java Bridge.

The problem is that Groovy isn’t the same as Java. Groovy adds lots of scripting features over and above Java’s current capabilities. Closures being one obvious highlight. This shines through if Groovy classes are used through a PHP/Java Bridge, it all just looks like Java. Take for example the following Groovy class that makes use of Groovy’s interceptors:

class Dynamic {
    def storage = [:]

  def invokeMethod(String name, args) {
  return "Hello World!";
  }

  def getProperty(String name) { 
storage[name] 
  }

  void setProperty(String name, value) { 
storage[name] = value 
  }
}

Another piece of Groovy can use this as follows:

foo = new Dynamic();
foo.bar();
foo.guff = "Hello World!";
print foo.guff;

Now see how this looks from a Java client:

public class Test {
  public static void main(String[] args) {
  Dynamic foo = new Dynamic();
  String result = (String) 
  foo.invokeMethod("sayHello", null);

        foo.setProperty("guff", "Hello World!");
        result = (String) foo.getProperty("guff");
    }
}

Yuck! And the same applies if we use this class in PHP through the Java Bridge. Well the PHP/Groovy Bridge solves this problem by integrating PHP and Groovy as first class citizens - see how it looks with the Groovy Bridge:

<?php

groovy_import("dynamic.groovy");

$foo = new Dynamic();
$foo->bar();
$foo->guff = "Hello World!";
echo $foo->guff;

?>

That’s better! Intuitive use of the Groovy class as intended. The groovy_import function is just one aspect of the PHP/Groovy Bridge. It also supports a range of other nice Groovy interop features including closures, currying and range objects. With this in mind I’ve put a slide cast up here where I ramble my way through an explanation of the PHP/Groovy Bridge.

The product documentation for the PHP/Groovy Bridge is also available here.

3 comments:

  1. Hi,

    Excellent tutorial. But, how can i integrate groovy into a simple Apache/Linux with PHP 5.3 ?

    It's a PECL ?

    ReplyDelete
  2. Hi, the PHP/Groovy Bridge does not run in any version of php.net - it runs as part of Project Zero (www.projectzero.org). Zero contains its own built in Web Server and an implementation of PHP which is compatible with php.net v5.2.

    We don't currently have any plans to upgrade the engine to be compatible with v5.3 as it is only in alpha at the moment. If this is important to you then please get on the Zero forums and lets the team know. We try to be as responsive as possible to feature requests!

    If you would like to try out Zero you will need the latest version of Zero, code-named Sebring. Please do check the license - I believe it it free to use in a production environment for up to four copies per location and on a machine that has no more than four cores.

    ReplyDelete