Advantages of Play Framework in Scala over PHP down the road

Prior to switching to Play Framework (in Scala), I was using CakePHP and Zend Framework (in PHP). It has been a great time developing with CakePHP but as the time passes I see lots of disadvantages of using PHP. Here is a list of what comes to my mind:

1. Deployment & Maintenance

The biggest issue I have right now with PHP projects is maintenance. I’m simply too scared to get software updates for my webserver! Why? Because I’m afraid that a PHP package or a PEAR package will get updated and I’ll have to modify some old code that I don’t want to touch. I had the same problem trying to run an old PHP project on my new laptop. I just couldn’t! Got a bunch of errors cause my new laptop had a newer version of PHP and some PEAR packages (Amazon AWS SDK for example) changed quite a bit and I had to modify all around the code which wouldn’t work if I would publish it on the server that uses the old PEAR package.

If the project would be still active & it would worth the time, I would spend it. But not all projects do.

When I switched to Play Framework, all these issues were gone! All the libraries I’m using are downloaded automatically by SBT (Scala Build Tool), in the correct versions and there is no need to install any extra package on the server. The only requirement is to install JVM, which is usually not a big deal.

Also, when the new version of Play Framework came out, I didn’t have to update all my projects. Some projects will just stay with the old version of the framework. And they’ll work just fine because all the libraries of the framework are contained inside the project. Even the Scala language itself! I have right now two Play Framework projects running on the same server and one is using Scala 2.9 and the other one Scala 2.10. And they work just fine because I didn’t have to install Scala on the server. It’s contained in the packaged produced by play dist (the Play Framework command that packages the code).

2. Having control over the code

CakePHP and various other PHP frameworks make it very easy to get started. You usually run a scaffolding tool (web-based or command line) that generates your model, controller & views. Then you go around and you modify them and very quickly you have a working product! Authentication works without any extra code, you have an ACL (Access-Control-List) that allows only logged in users to access some pages and other pages are restricted only to admins. Everything seems fine! But what happens further down the road?

Further down the road things get a bit trickier:

  1. The auto-generated forms will not be sufficient and you’ll have to dive deep into the framework to find how to replace them with more verbose code that allows you to change some parameters
  2. The authentication will need some extra tweaking to redirect users to different urls depending on the user’s role and you’ll wonder how to do this since there is no code for authentication in your files!
  3. You’ll need some more precise authorization mechanism to prevent user’s from accessing each other’s data. And what will you do? Do you really want to put that “if” in every action in your controllers?
  4. At some point your interaction with the database will become a bit tricky. You’ll realise that you need to speed up some queries. And you open your model’s file and you see no code in it! Then you have to dive deep into the framework to learn the database mechanisms and how to use them. And also how to switch off all that magic that loads all the other dependent models on each request, where you don’t really need them.

With frameworks like Play Framework, things are a bit different. You start with an empty project. No scaffolding, no auto-generated codeno database magic. You need to learn how to access the database to create your models, then learn how to generate forms and then how to write the authentication and authorization code. It’s quite a bit of work telling the truth and might seem unproductive at the beginning. But further down the road things are a lot better:

  1. You can do any modifications you want on your forms. All the code is there and you know where to find the documentation.
  2. You can do all the tweaks you want on your authentication. The code is all there and you wrote it. Maybe you copy-pasted it but you still know where it is in your code.
  3. You can also modify the authorization mechanism and find really elegant ways of expressing it. There is absolutely no need to add that “if” to every action!
  4. You’ll also not have any problem with the database. You wrote all the queries and all the model’s code. It’s all there. Do any changes you need and speed up those slow queries.

Ok. That’s it about having control over the code. I think you got the point.

3. Refactoring

This subject is rather lame but it’s good to mention it. You already know that Scala is a compiled language where PHP is an interpreted language. Therefore if you modify some code in Scala you’ll get a bunch of compile errors before you’ll be able to run again your website. And Play Framework will show you exactly on which files and which line the error is. It’s a bit of trouble sometimes but once you fix the errors you have a bit more peace in your mind that the code is working as it should. You should still test it of course but at least you are not afraid that your client will open a page and see a syntax error!

With PHP things are different. You change the code and then you have to go around the website and see if everything works fine. You’ll tell me now that it’s the same with Scala but it’s not. With Scala you at least know that when a function is expecting a String, it’ll receive a String and not an array. But with PHP you don’t know that. You have to do more tests to make sure of that. So watch out with PHP.

Ok. That was lame. Compiled Vs Interpreted. We heard it 1000 times.

What I really wanted to say about refactoring is that with Play Framework and Scala you can do lots of simplifications on your code. What do I mean? Let me give you some examples that come to my mind:

  • In your actions for users you want to have quick access to the user model. Then you define a method def user(implicit req:Request[AnyContent]) that implicitly takes the request and returns you the user stored in the session. Then all around your actions you can easily access your current user.
  • You can simplify your validation code and create really readable and elegant code. See this blog entry about it.
  • You can create a toJson method on your models that produces a json version of your object with the values you want to include by combining information from other models etc.
  • Simplify your models by creating a base model with methods like find, update, insert, delete etc. I know these are provided by the PHP frameworks but here you can easily modify them to optimize them whenever you want.

It’s a lot more things than what I wrote here but I can’t recall all of them right now. I hope you got the point.


Since I switched to Play Framework (in Scala) from Cake PHP a year and a half ago, I have enjoyed coding a lot more! I’m already quite efficient with Play and I can find endless way to simplify my code and make it really neat and readable. And deployment is going really well! No issues with the server updates, websites run super fast and barely having any downtime. I’m really happy with Play and Scala and I really want to see more people using it! Not only because it’s fun & very productive environment but I also want it to be easier to find people to work with on Play projects 🙂 The more people get to know Play and Scala the less arguments I’ll have to bring in to convince them to use it on a new project with me 🙂

Ok .. I wrote a quite a lot today! Need to get back to work!