Reputation: 7942
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.
Upvotes: 8
Views: 23177
Reputation: 21
I can only find two:
1) https://github.com/bafolts/java2php
2) http://www.runtimeconverter.com/
There is also Caucho Resin, but it is not exactly a converter. It is an implementation of php inside of Java.
There is a whole lot on the internet about numiton, but their website is down for several years now.
Upvotes: 0
Reputation: 41
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.
Upvotes: 3
Reputation: 21
I would normally take the class generated by php5servlet, a jar thats available in tomcat & resin.
Then change the class file to Java.
cheers
Upvotes: 2
Reputation: 14964
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.
Upvotes: 1
Reputation: 41132
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.
Upvotes: 13
Reputation: 88796
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!
Upvotes: 2
Reputation: 15927
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.
Upvotes: 0
Reputation: 1212
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.
Upvotes: 0
Reputation: 105878
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.
Upvotes: 0
Reputation: 117477
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.
Upvotes: 1
Reputation: 12257
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.
Upvotes: 0
Reputation: 28062
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.
Upvotes: 11