While I personally agree with some of what Luke Welling wrote in his article "PHP is not Java" for PHP Advent 2008, I also disagree with the general tone of the article and the examples he provides. The example Luke provides might be true in some respects (that it looks like Java code), but the solution offered is the best example why PHP and PHP developers are looked down on most time.
PHP is the red-headed step child (no offense to redheads, I love you all) when it comes to programming, or wait, scripting languages. Because PHP is not even a programming language. ;-) For example, when I started at this co-working place most of the people did not talk to me at all. Besides Jan, who I share the room with most of the time, there are only Ruby (and Java) people on this floor. Horror, right? (Just kidding!)
And even though my crap PHP code handles much more traffic than their fancy rails, and even though I'm open to other languages as well, most people stop taking me serious when I admit to doing PHP.
The truth is out there, Mulder.
The notion of the last years has been that a lot of PHP-based open source projects worked hard to improve the quality of their code by defining coding standards, by improving the documentation and sometimes even by adding a dedicated QA team. Even though any PR is good PR, no one wants to make those headlines. The (by no means complete) list of examples include (of course) PEAR, Joomla (where this is an ongoing effort :-)), and also rather notorious projects such as phpBB and Wordpress.
Or take a look at projects such as ezComponents or the Zend Framework — (besides clean IP) the idea is to provide a framework written in higher quality PHP code. Let's not argue if one of those projects actually achieved this goal (yet), or if they struggle all together — I'm sure, we are all on our way.
What Luke suggests to make PHP easier is the exact opposite. Yeah — let's use globals again (because we are so experienced) and let's not define a clear API to whatever we use. While design patterns are sometimes questionable and using design patterns during job interviews can backfire like hell, they also have their use cases when used wisely. I'd like to believe that Luke's suggestion works when you work in a team of very skilled developers, but I think even the very skilled jedis slack off sooner or later.
Take into account that the project might grow beyond one PHP file (I've seen it, I swear!) and you've got yourself in a situation to personally keep track of variables in the global, etc.. No, thanks! There are multiple horror scenarios which I think of, if I really followed his suggestion.
In direct response to Luke's example — I personally hate object factories — I find the singleton pattern an alright way to provide an API to how to create an object in your code base. Especially if you want to enforce that there's just a single copy of the object around (or maybe just up to four or 21 copies). The singleton might not be the fastest or the most obvious way for the beginner PHP developer, but I personally think it's a solution.
And if you take those five minutes to explain the idea — that's even better.
Point taken, the singleton limits you and there are other ways to write good code without using a singleton pattern, but that's really beyond the scope of this blog post. Design patterns in general can make sense and should be used responsibly, and just because the implementation looks like Java, that doesn't make it any worse.
We developers are often guilty of a hefty prejudice towards other languages. For example us PHP people tend to throw PHP at everything because it works. Nothing wrong with that, but at the same time we also forget to look for a more appropriate solution somewhere else. One of the languages in the cross-hair of our disgust is always Java ("It's complicated!") followed by Ruby and the on Rails framework.
Please take note that there are a lot of different concepts in programming (or scripting :-)) and borrowing something that works in one language and implementing it in another does not always play out nice, but sometimes it just does.
At other times, just learn a new language and get over it. You can still PHP away! No one asks you to hate PHP because you did a little Java or Python. It will make you a much better developer once you looked across the edge of the plate and tried something else. At least you will have a more profound opinion on why something sucks, or maybe you realize that it doesn't always suck.
The author does not allow comments to this entry