Over a million developers have joined DZone.

Grails for PHP developers II

DZone 's Guide to

Grails for PHP developers II

· Web Dev Zone ·
Free Resource

Michael Kimsal delivers the second part of this series. This time he shows groovy examples alongside their PHP counterpart, which will surely help get acquainted with the syntax.

Backing up

I may have got a bit ahead of myself in part 1, as it was pointed out to me that I didn’t explain enough about what Grails is, and why I was interested. I thought I’d done that, but obviously not enough, so here’s a bit more…

Grails is a convention-over-configuration web framework similar in spirit to Ruby on Rails. One of the driving ideas behind Grails, as far as I can tell, is to simplify the majority of the web development process. The ‘conventions’ include standard naming practices, standard locations for common files (domains in the /domains directory, controllers in /controllers, etc.).

Grails is a Java web framework, but uses the Groovy language for most of its dirty work under the hood, although some of the underlying Grails code is apparently ‘pure’ Java. Groovy itself is, well, a bit Groovy. It is a dynamically-type langauge, and gets rid of the need for a lot of the repetitive crap which scares away people from Java.

Hello world

Let’s see a code comparison. In Java, the standard “hello world” resembles this:

class helloworld {
public static void main(String args[]) {
System.out.println(”Hello World!”);

and to run it you’d first compile the above helloworld.java with javac in to a class file:

   javac helloworld.java

then run the class file

   java helloworld

In Groovy, you’d create a file

println “Hello World”

Then execute it with

   groovy helloworld.groovy

Much easier, agreed? Looks very similar to the good old PHP ‘KISS’ approach:

< ?php echo “Hello World”;?>

which we’ve all come to know and love over the years. Don’t get me wrong here though, the Groovy language isn’t terribly similar to PHP, although the curly braces and semicolons are there (actually the semicolons are optional).

Thrown for a loop

I’m going to jump in here with some code comparisons between PHP and Groovy which will help illustrate some of the differences.

< ?php
$names = array("Ramesh","Ashish","Keith");
foreach($names as $name) {
echo $name."\n";
def names = [’Ashish’,'Keith’,'George’]
names.each { println it }

The ‘each’ method iterates over the names array and passes the data to the closure - the block of code inside the curly braces. The it inside the closure is an automatic name given to the piece of data currently being iterated over. We could name the iterated value inside the closure like so:

def names = [’Ashish’,'Keith’,'George’]
names.each { name->
println name

We can get the positional value of where we are in the index with a slightly different method. Instead of each we use the eachWithIndex.

def names = [’Ashish’,'Keith’,'George’]
names.eachWithIndex { name,number->
println name + ‘ is number ‘ + number

Note the ‘number’ - the positional index counter - comes after the iterated value name. This hosed me up at first a bit, as I’m used to the old $key=>$val idiom from looping in PHP.

None of the examples here are meant to imply that this is the only way of looping in Groovy. Indeed, the standard while loops and whatnot are all available - this page has far more detailed information than I can put up here. This section was meant to illustrate iterating with closures. I’ll go in to closures in a bit more detail later, but they are probably one of the key differences between Groovy and PHP. As you might surmise from the code above, the foreach and while loops used to iterate over data structures in PHP aren’t typically used when iterating over data structures in Groovy - that’s typically handled in closures.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}