You are viewing...

Transitioning from PHP to Java

Updated on October 26, 2012 at the 07th hour
Posted under:

DISCLAIMER: Expressed views on this blog are my own.

[UPDATE 10/27/2012] Writing servlets isn't as hard as I thought. Everything is just baked in, though I'll have to obviously get more experience with J2EE classes and the other components before I can make a definitive decision on just using Java. My next big concern is whether there are any xss/string stripping methods or whether I'll outsource that task to a library.

I think it is about that time I started looking at Java as legit language for the web. There are many companies that use it successfully without a huge hassle like PHP. Java has application servers built for serving backend purposes, so that leads me to think of using Java (Apache Tomcat or another variant) to power Excerion Central rather than PHP. Sure I've built a good amount of Excerion Central in PHP already and I'll invest more time to finish it, but then I'll have to start translating that PHP I've written into Java, so there may be some lag time in some updates, but it will be worth the benefit. What are the benefits you say?

One thing that's bothered me about PHP for a very long time is the fact that it forks a new process for every request. That is inefficient for backend tasks especially if you launch and magically it becomes viral. You'll have a php process that forked over and over again and that is total crap.

For the past awhile now, I've been looking at alternatives to solve that issue such as using Erlang to handle certain backend tasks and using its actor model to create instances of actors which have a one to one mapping to a session. What is most beneficial about this is that there is no longer any reason to contact the database every request, we can update it right into memory. This also means that you have to save the session back to the database periodically and to the local disk (more frequently). The decreased pressure on the database is a benefit because the number of reads and writes dramatically decreases decreasing the probability of a race condition or delayed writes in the database.

With Java or the JVM rather, I have access to Scala and that means I do not have to learn a whole new language to get the gears turning. This kicks serious ass because Scala has the actor model baked in as well even though it isn't as lightweight as Erlang's, I will be content with the negligible performance hit. With Java, you are not limited in what you can do and I think I've actually increased what I will and will be able to do by looking towards switching out of PHP.

This does not mean I am abandoning PHP, this is purely a business decision not a personal one (maybe it sort of is). I've indicated in the past that I'd move towards using JavaScript for UI/html generation because it puts the client's CPU to work rather than process html strings on the server and use bandwidth to transfer that information. This severely limits PHP's usefulness. I will still use PHP for projects, which is always good, but I believe I've outgrown it after 8-9ish years of it. I have been trying to get a job that requires me to proactively use another language such as Java to get things done. Apache Mahout has been somewhat great in introducing me to using/deploying war files.

This is just another step I'm taking. Java has it's own mysteries, but it is a pretty frickin' amazing/versatile langauge.
You just read "Transitioning from PHP to Java". Please share if you liked it!
You can read more recent posts here.