PHP, Groovy and language evolution

I’ve been using PHP since early 1996, back in the PHP/FI days.  I’ve used it steadily over the years, sometimes full time and sometimes part time, but that’s been pretty much a primary language for me for 12 years now.  During that time I’ve seen a lot of language changes, some for better and some not so much.

I remember when PHP5 first came out having ‘discussions’ with a number of people who insisted that PHP5 was way better than 4.  I still couldn’t see too many things that PHP5 made possible that were not possible in PHP4.  Streams made some data processing nicer, and interfaces made it easier to code which was a bit more self-explanatory.  Basically, intentions were made clearer with things like “public protected private” (PPP), but I have not yet seen any web project get done faster or dare I say even much *better* due to those sorts of things.

PPP stuff can make sense if you’re going to be distributing code to others.  For example, if you have sensitive properties that might break your code if futzed with.  However, the majority of code written is inhouse, and to be used by people who have access to the code and can make changes to visibility properties anyway.  Your code won’t break if I change your ‘private’ or ‘protected’ to ‘public.  But keeping some of your properties as ‘private’ can make life more difficult for people who have to live with your code later (AKA me!).  I had Smarty break on me because I dared to put {$comment->postedDate} in a template, instead of {$comment->getPostedDate()}.

Smarty breaking basically means white screens, so after a few minutes of tracking down the error, I found out the original author had made all the properties private and made get/set methods for them.  This was not code meant for others, nor documented in any other way.  IDEs certainly can’t deal with these sorts of things very well, and there’s no way an IDE will ever be able to give me ‘code complete’ in a Smarty template for potentially random variables passed in.

During all this, I couldn’t help but think the original developer probably thought they were “doing the right thing” or being “object oriented” by taking the “private and use get/set methods” approach.

I’m not all doom and gloom, or 100% negative on PHP5.  Native JSON and better XML support (DOM and SOAP, for instance) *are* examples of things I saw as genuine improvements in the language.    Those sorts of improvements remain true to PHP’s original foundation of being a language focused on the web space.  Striving for more “OOness” in PHP feels like it’s trying to become more of a general purpose language like Java, but going about it rather clumsily.

This isn’t specifically saying PPP is necessarily bad, but that PHP could have addressed the issue in a fashion more suitable to dynamic languages.  Keep reading for an example.

Over the last year or so I’ve been spending more time in the Groovy/Grails world as well as PHP.  I’m not a Java person by trade, and more than a few people have asked ‘coming from PHP, why not go right to Ruby for dynamic stuff?’.  There’s a few reasons for my interest in Groovy.  First off, I had great intros to the technologies by Jason Rudolph and Scott Davis at our local Java user group last year which definitely sparked some interest then and there.  Additionally, GORM and Grail’s ‘domain first’ approach to application development feel more natural to me than other technologies.  I’m by no means a master of these technologies, but I’m learning more as I go along.

Groovy is a dynamic language built with inspiration from Smalltalk, Ruby, LISP and other languages.  It compiles down to Java code, and co-exists with Java beautifully.  It seems to exist largly by and for Java developers trying to achieve more rapid development without sacrificing their Java investment.

One of the key things that Groovy does is ignore PPP notations.   By default you do not need to indicate any visibility for class properties – simply give a type.

class foo {

String name

Date createDate


etc.  By default, all properties are private, but respond to dynamically created get and set methods.  In the example above, getName() and getCreateDate() would automatically be generated in to Java byte code for you (as well as setName and setCreateDate()).  You can override these if need be, but the defaults are pretty sane.  Rather than forcing the developer to write (or an IDE to generate) get/set methods for every property, the compiler handles access control under the hood with consistent default behaviour.

PHP could pretty easily implement something like this, and it would be a great productivity boost for people that need to refactor code to implement new features.  Rather than having to go through existing code and change *every* instance of $foo->bar to $foo->getBar(), just write your own getBar() method in the main class, which would override the default behaviour.  Same thing could be done for setting values too.

Thinking about this closer, I’m seeing Java developers *migrating away* from PPP in an effort to be more (dare I say?) agile.  Perhaps agile isn’t the right word – productive is probably more accurate.  Groovy also does away with the static typing requirement – just declare something as ‘def’ and run with it.

Said another way, people who’ve had some of the constructs (“high ceremony” as Stu from Relevance might say) for years are actively looking to get rid of them and move to something more productive.

Contrast this with where many PHP developers’ heads are at, with wanting more type hinting in their method signatures, embracing PPP all over the place, and trying to adopt what they see as ‘best practices’ from other languages.  At the same time, developers from other languages are having second thoughts about the necessity of some of their dogma, and are looking at dynamic languages like Ruby for inspiration.   Sadly, most don’t look to PHP for inspiration, but I think working in PHP, and thinking dynamically, can be a great advantage if dealt with properly.

Is this just a case of ‘the grass is always greener on the other side’?  What do you think?

I'm currently working on a book for web freelancers, covering everything you need to know to get started or just get better. Want to stay updated? Sign up for my mailing list to get updates when the book is ready to be released!

Web Developer Freelancing Handbook

Share and Enjoy:
  • DZone
  • Facebook
  • Reddit
  • StumbleUpon
  • Digg
  • Simpy
  • Technorati

Tags: , , , , , , , , , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">