views:

3171

answers:

14

I have been tasked with converting several php classes into java classes, which is quickly becoming a nightmare for me. I understand the basic language structure, it being similar to C. It is all of the function calls and class calls that seem to go nowhere and the fact that a var can be declared in the !middle of an expression! that is spinning my head, oh and the fact that there is zero "0" documentation.

What is the best method (and/or) tool (and/or) reference material to convert the php into java code?

edit: There is 3 reasons that I am having to convert the php to java.

  1. The usual reason, my boss told me too.
  2. The php is too slow, it is taking minutes sometimes to run a request to the server.
  3. php is a nightmare to scale and maintain.(at least for us strong typed language types)
+7  A: 

A human is the best tool.

I would try to rewrite the php to remove most of the php features to something C like. Then you'll have an easy time rewriting in Java.

But I need to ask, why do you need to convert the php? Can you not wrap the php into something callable from Java? This way you won't add any errors while converting it.

Pyrolistical
the php is too slow.
WolfmanDragon
A: 

I can not imagine that a tool for this is existing. I did something similar with C++ und Java. It is a pain, but the best is to impement it by your self.

Or write it in C and create a dll with a jni warpper to call it from Java. This should be the fastet way.

Markus Lausberg
+1  A: 

Depending on the PHP code, this may be an almost impossible task. The other way around is much easier. PHP is a very dynamic language, and you can get away with things that are impossible in Java. One particularly disruptive thing is that a PHP variable may change type during execution. This is rarely used though, but it could happen. In addition, since PHP is loosely typed, there are a lot of implicit conversions. Some are coincidental, while others are important for the meaning of the program. And then there is the fact that PHP code isn't strictly object oriented, like Java is. Even in object oriented PHP programs, you will usually see some degree of procedural elements. These can not be mapped directly to Java code.

As Pyrolistical, I'm curious as to why you need to convert this code? If it's legacy code, wouldn't it be better to keep the PHP code running, and interface with it through some kind of service interface (SOAP, RPC, others)? You could then gradually replace parts over time.

troelskn
What you described is exactly what i am running into. I don't have to do a line by line rewrite, as long as the java does the same thing as the php, everything is good. The PHP is too slow. Not only does java run faster(near the speed of C) it also has better caching.
WolfmanDragon
caching tools i should have said.
WolfmanDragon
You know better than I, obviously, but PHP is fairly decent performance-wise. Maybe you would be better off optimising what you have?
troelskn
A: 

You could probably write something with the Reflection API to do some of this, but you really couldn't do anything with function bodies - you'd end up with stub classes that have no implementation.

Peter Bailey
+1  A: 
  1. throw code away
  2. rewrite in java
  3. ????
  4. profit!
A: 

I've been looking into Groovy as a transition language from PHP to Java. They (the Groovy developers) claim that it compiles to java byte code the same as Java code would.
It's also less strict, they have several examples of translation on their website.

charlesbridge
I'll check it out. thanks
WolfmanDragon
A: 

It sounds like you're trying to convert PHP code that is procedural into an OO code base.

This is not so much a question of PHP to Java, but rather a paradigm shift. There's no automated way to do it, it's going to be rough. Especially if one code base is badly written.

Btw, I would also why are you converting? Is it just performance? And if so, is there nothing you can do to fix the performance issues. I don't think just converting from one language to another will fix it, you'll still have to find the bottleneck.

Stephane Grenier
The PHP is written OO with procedural code gluing it together. With no documentation and 3 letter var names. yes there is more than one bottle neck, but mine job is just too fix the code bottleneck.
WolfmanDragon
@WolfmanDragon: the bottleneck isn't for the difficulty of understanding or the look of the code, but the quality of code: speed bottleneck is often in database handling, for example.
PhiLho
Yes, that is a different bottleneck altogether. The bottlenecks have been located. The PHP is a bottleneck, Later goes the SQL.
WolfmanDragon
Without seeing the code I can only guess, but I find it difficult to believe that the language is the bottleneck (and I'm a Java fan!). Sure you can get better performance and maintenance with Java, but a rewrite like you're describing sounds pretty scary and I can't see it being that much better!
Stephane Grenier
My guess, and it's only a guess so please take it as such, is that the PHP code is doing a lot of SQL calls. At least that's been my experience with PHP code. Most PHP developers just seem to call the DB whenever and don't pay particular attention to the costs. Not all PHP developers, but a lot.
Stephane Grenier
+1  A: 

I said this in the PHP Optimization Tips question and I'll say it again here: If you're running PHP from a static environment (web server module or FastCGI), use an opcode cache, such as APC. Otherwise, PHP is reinterpreting/recompiling your code on every request!

R. Bemrose
+6  A: 

You ask about best practices. I believe a good practice in your case is the approach pleasantly presented by theman: using an automated tool will probably give a bad result: garbage in, garbage out...

You have the code: analyze it, in its broad lines if necessary. And re-create it in Java. It might be time-consuming, but not necessarily worse than by doing blind conversion. And you can document on the way, and perhaps use this analysis to find the problematic parts.

PhiLho
Thank you. That is what I am doing at the moment. I lack the understanding of dynamic languages and was hoping for at least some reference guides. I guess that I will have to learn to program in PHP to do this. [Shudder]
WolfmanDragon
+1  A: 

For completeness, I should point out that there is a PHP runtime for the JVM. Check out Quercus.

You might consider leaving your current codebase in PHP and just getting it to run on the JVM. You can then rewrite code in Java as needed.

ykaganovich
+2  A: 

Check out http://www.numiton.com

Thank you for your link. That phase of the project is now done, but it may be of help to someone else. I will upvote it for that reason alone.
WolfmanDragon
A: 

Caucho/Resin server converts PHP Code into Java servlets in run-time!

A: 

I would normally take the class generated by php5servlet, a jar thats available in tomcat & resin.

Then change the class file to Java.

cheers

Gunesh
A: 

Someone implied that Java is not as flexible as PHP : it is, by default, far more flexible actually (in that the core API contains thousands of classes and built-in functionality). You just have to learn the core concepts of both languages, such as autoboxing for Java, for example, to make room for dynamic types. Check out http://www.javaworld.com and I am currently working on porting a big API from PHP to Java, should take me a few days. Two classes, libcurl, json parsing, and maybe a hundred methods / functions.

Argo