Grails for PHP developers part 5
Focus on Groovy
I almost initially called this series “Groovy for PHP developers”, but decided that because Grails was really the ‘web’ portion of the Groovy/Grails combination, and that PHP was very much web oriented, I relented and went with “Grails for PHP developers”. However, in retrospect, this may have been a mistake. Why?
Primarily because you’ll spend much of your time in Grails writing Groovy code (or Java, perhaps). As such, a good understanding of the language is essential to get the most out of a Grails project. So, with that in mind, this installment is going to cover some of the basics of the Groovy language. We’ve covered a bit on arrays before, and we’ll revisit them again today, along with other Groovy goodness.
Back to basics
Groovy is about as basic a language to get started with as any. For today, rather than running things from the command line, I’ll show some screenshots of the Groovy console. If you have Groovy installed, you should just be able to type ‘groovyConsole’ and get a popup window looking something like this:
This is a small interactive console which is handy for trying some quick concepts, as well as doing a bit of on-the-fly inspect of variables (more on that later)
Setting variables
We’ll start off with something slightly more advanced than “Hello World”: we set a variable and then print it out like so. Hit CTRL-R or use the “Script” menu to run the code after you’ve entered it below.
Neat, eh? All we did was assign 3 to “a” then print it out. No $ around a variable name - arguably simpler than PHP! However, Groovy’s simplicity can be as much of a potential headache as PHP’s. Here’s how.
If you’ve ever done Java before, you’ll may have run in to the issue of redefining a variable’s type. A variable that’s “x” holding a number then later holding a string. Java’s strict enough to prevent this. It may have been what you intended, or perhaps not. Java won’t allow it. It’s fairly common in the PHP world however. I’m certainly not claiming this is a good thing to do, but if you’ve dealt with PHP code for a while, you’ve likely come across this practice. It’s especially prevalent in procedural code that reuses the same few placeholder variable names for different purposes. And, for better or worse, Groovy allows it as well. Example:
So, word of warning - be as careful with your variables in Groovy as you need to be in PHP.
It’s array-ning, men
PHP has one basic type of array structure, which can be used for both zero-based indexed arrays and associative arrays. Groovy splits these up in to two different concepts: lists (zero-based arrays) and maps (associative arrays). A standard list definition looks like this:
We simply put the elements we want in the [ ] markers, and separate with commas. It’s not *that* different from PHP’s syntax ($a = array(8,6,7,5,3,0,9) ), but is a bit less typing. A difference from PHP you’ll notice right away is when we print out an array - we get the elements in the array, not the help PHP goodness of “Array” dumped to the screen. Yes, var_dump() and print_r() are your friends in PHP, but it can get a bit tiresome.
Because a list (and a map, and pretty much everything else in Groovy) is an object, the syntax for manipulating the list is object oriented. Let’s join the list elements together.
Notice the println this time instead of print? We’re printing the data and adding a line feed afterwards. In the example above, we join the elements of the list together first with nothing between them - we simply get 8675309 as a list of numbers. By changing the join parameter to “-” we get 8-6-7-5-3-0-9. Same functionality as PHP’s join (and alias implode) but syntactically different.
What do maps look like? Similar, but with keys in them.
Notice the key name precedes the value with a colon (:) as the separator, instead of PHP’s “=>” syntax. Also notice that the keys do not have quotation marks around them. You *can* have them around, but Groovy doesn’t seem to mind either way. The text values do need quotation marks around them, however.
Groovy allows for n-level nesting of maps and lists as well. Check out the example below:
Let’s try to redefine the ‘name’ element of our map. If we do not use quotation marks in this context, we’ll get an error.
See the error about a Missing Property? What’s going on under the hood is that Groovy is compiling the script down to a Java class file, and all variable references become properties of the class that was created for us by Groovy (we didn’t declare any class - we just have a few lines of script). Because there’s no value for ‘name’ Groovy is assuming that we want the ‘name’ which would be part of the class. It’s as if Groovy is looking for the PHP equivalent of $this->name (which would be this.name in Java/Groovy) and because we haven’t set it - indeed, it doesn’t exist! - Groovy throws this Missing Property exception. To get around this, we have to use quotation marks, as so:
We can also use variables as keys, like so.
There’s more with variables and arrays I’d like to dig in to, but I need to save that for a future article. In the meantime, have fun getting started with lists, maps and variables, and send in any questions you have about Groovy. I’ll do my best to answer them.
Did you like this post? Buy me a hot chocolate!











