What is the best way to convert an existing php class into Java?

前端 未结 15 1257
梦毁少年i
梦毁少年i 2021-02-09 14:07

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 simil

相关标签:
15条回答
  • 2021-02-09 15:04

    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.

    0 讨论(0)
  • 2021-02-09 15:05

    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.

    0 讨论(0)
  • 2021-02-09 15:06

    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.

    0 讨论(0)
提交回复
热议问题