将现有 php 类转换为 Java 的最佳方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/363805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the best way to convert an existing php class into Java?
提问by WolfmanDragon
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.
我的任务是将几个 php 类转换为 java 类,这很快成为我的噩梦。我理解基本的语言结构,它类似于 C。它是所有的函数调用和类调用似乎无处可去,并且可以在表达式的中间声明 var 的事实!这让我很头疼,哦,还有零个“0”文档的事实。
What is the best method(and/or) tool(and/or) reference materialto convert the php into java code?
将 php 转换为 java 代码的最佳方法(和/或)工具(和/或)参考资料是什么?
edit: There is 3 reasons that I am having to convert the php to java.
编辑:我必须将 php 转换为 java 有 3 个原因。
- The usual reason, my boss told me too.
- The php is too slow, it is taking minutessometimes to run a request to the server.
- php is a nightmare to scale and maintain.(at least for us strong typed language types)
- 通常的原因,我的老板也告诉了我。
- php 太慢,有时需要几分钟才能向服务器运行请求。
- php 是扩展和维护的噩梦。(至少对于我们强类型语言类型)
回答by PhiLho
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...
您询问最佳实践。我相信在你的情况下,一个好的做法是 theman 愉快地提出的方法:使用自动化工具可能会产生不好的结果:垃圾进,垃圾出......
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.
你有代码:如果有必要,分析它,在它的广泛领域。并在 Java 中重新创建它。这可能很耗时,但不一定比盲目转换更糟糕。并且您可以在途中进行记录,也许可以使用此分析来查找有问题的部分。
回答by Pyrolistical
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.
我会尝试重写 php 以将大部分 php 功能删除为 C 之类的东西。然后,您将可以轻松地用 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.
但是我要问,为什么要转换php呢?你不能把 php 包装成可从 Java 调用的东西吗?这样你就不会在转换时添加任何错误。
回答by Powerlord
I said this in the PHP Optimization Tipsquestion 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!
我在PHP Optimization Tips问题中说过这一点,我会在这里再说一遍:如果您从静态环境(Web 服务器模块或 FastCGI)运行 PHP,请使用操作码缓存,例如APC。否则,PHP 会根据每个请求重新解释/重新编译您的代码!
回答by Powerlord
- throw code away
- rewrite in java
- ????
- profit!
- 扔掉代码
- 用java重写
- ????
- 利润!
回答by Powerlord
Check out http://www.numiton.com
回答by Argo
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.comand 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.
有人暗示 Java 不如 PHP 灵活:默认情况下,它实际上灵活得多(因为核心 API 包含数千个类和内置功能)。您只需要学习这两种语言的核心概念,例如 Java 的自动装箱,就可以为动态类型腾出空间。查看http://www.javaworld.com,我目前正在将一个大型 API 从 PHP 移植到 Java,这需要我几天时间。两个类,libcurl,json 解析,可能还有一百个方法/函数。
回答by Argo
Caucho/Resin server converts PHP Code into Java servlets in run-time!
Caucho/Resin 服务器在运行时将 PHP 代码转换为 Java servlet!
回答by Gunesh
I would normally take the class generated by php5servlet, a jar thats available in tomcat & resin.
我通常会使用 php5servlet 生成的类,这是一个在 tomcat 和树脂中可用的 jar。
Then change the class file to Java.
然后将类文件更改为Java。
cheers
干杯
回答by ykaganovich
For completeness, I should point out that there is a PHP runtime for the JVM. Check out Quercus.
为了完整起见,我应该指出 JVM 有一个 PHP 运行时。看看栎。
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.
您可能会考虑将当前的代码库保留在 PHP 中,而只是让它在 JVM 上运行。然后,您可以根据需要用 Java 重写代码。
回答by troelskn
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.
根据 PHP 代码,这可能是一项几乎不可能完成的任务。反过来说要容易得多。PHP 是一种非常动态的语言,您可以摆脱在 Java 中无法实现的事情。一件特别具有破坏性的事情是 PHP 变量可能会在执行期间更改类型。虽然这很少使用,但它可能会发生。另外,由于 PHP 是松散类型的,所以有很多隐式转换。有些是巧合,而有些则对节目的意义很重要。还有一个事实是,PHP 代码并不像 Java 那样严格面向对象。即使在面向对象的 PHP 程序中,您通常也会看到某种程度的过程元素。这些不能直接映射到 Java 代码。
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.
作为 Pyrolistical,我很好奇您为什么需要转换此代码?如果是遗留代码,保持 PHP 代码运行并通过某种服务接口(SOAP、RPC 等)与之交互不是更好吗?然后,您可以随着时间的推移逐渐更换零件。

