Does anyone know of a (preferably open source) PHP to ruby compiler? i.e. a program which parses PHP code and produces semantically equivalent ruby code?
-
Anything like that will produce damn ugly Ruby code - Ruby is so much more expressive than PHP. Why do you want to do this? – Skilldrick Oct 06 '10 at 15:01
-
Well yes, it don't expect it to rival hand-crafted Ruby but the expressiveness of the language you mention should only make it easier to implement PHP semantics. I'm considering options for migrating a large PHP code-base to ruby, a tool like this would make this much easier. – Ramon Oct 06 '10 at 15:29
-
1You don't want to use a compiler's output to produce code that you will maintain. People that are compiling generate (rightly) whatever works (ugly or unmaintainable is irrelevant) if the generated code executes correctly. This why such compilers have terrible reputations as "migration tools"; they aren't! Migrating a PHP application into *maintainable* Ruby is an entirely different problem. – Ira Baxter Oct 09 '10 at 23:39
6 Answers
This is a pretty daunting task already. And you picked two languages that are very dissimilar. Sure, you could probably programatically translate PHP to Ruby, but the resulting code would be very un-ruby-like.
See some related questions and their answers:
- How to translate between programming languages
- Why is it not possible to create a practical Perl to Python source code converter?
If you could translate idiomatic PHP to idiomatic Ruby you probably need human-like intelligence - ie: the ability to understand what code does (in essence) and rewrite it in the target language. Unfortunately we don't have very smart AI in this front. At least not that I'm aware of.

- 1
- 1

- 83,810
- 28
- 209
- 234
-
They are more similar than, say PHP and C++ and people have developed compilers for PHP that target C++, albeit for a different purpose - Facebook's HipHop does this I believe. Syntactically, yes, they are totally different which is why you would need a full-blown compiler and not some sort of "transliterator". – Ramon Oct 06 '10 at 15:35
-
@Ramon PHP is written in C after all, and many of its functions are just wrappers to C functions. See the second link for reasons why it would be extremely difficult to create such "compiler." – NullUserException Oct 06 '10 at 15:38
-
@Ramon: Compiling PHP into Ruby would likely be an effort similar to that of compiling into C++, with the added work of implementing all those C libraries on which PHP draws (the C++ implementation just uses those C libraries). So, if you want to expend the same amount of energy as Facebook did to get a PHP to Ruby compiler, I'm sure you could do it. You might find out that's quite a lot of work. – Ira Baxter Oct 09 '10 at 23:36
I haven't used it myself, but you may want to look at Phuby. It allows you to run PHP code within ruby. Then I'd try to unit test the life out of the code (using ruby unit tests), and then re-write the code in ruby.
However, I suspect Phuby isn't even remotely production-ready.

- 78,473
- 57
- 200
- 338
-
1Yeah embedding is complicated, which makes Phuby far from production-ready. Why not just call externally to a php web service? – Minqi Pan Aug 19 '12 at 07:15
I don't think one exists.
A combination of Quercus, which re-implements PHP in Java, and JRuby might be of help, depending on what you are trying to accomplish.

- 1,120
- 11
- 9
-
Interesting idea, but that sounds like more a way to run PHP code together with Ruby code on the JRuby runtime - not a bad idea as the production Quercus compiler is not open source and JRuby seems to be leading the way in terms of non-Java languages on the JVM. – Ramon Oct 06 '10 at 15:31
Using the universal-transpiler library for SWI-Prolog, you can convert a subset of PHP into Ruby and several other languages. This is an example program with PHP source code as its input:
:- use_module(library(transpiler)).
:- set_prolog_flag(double_quotes,chars).
:- initialization(main).
main :-
Input = "function add($a,$b){return $a.$b;}function squared($a){return $a*$a;}function add_exclamation_point($parameter){return $parameter.\"!\";}",
translate(Input,'php','ruby',X),
atom_chars(Y,X),
writeln(Y).
This is the program's output in Ruby:
def add(a,b)
return a+b
end
def squared(a)
return a*a
end
def add_exclamation_point(parameter)
return parameter+"!"
end

- 30,230
- 67
- 195
- 328
http://railsforphp.com/reference/ can be a handy reference when you're just getting started going from PHP to Ruby. You can look up a PHP function and find its Ruby equivalent.

- 15,382
- 6
- 40
- 45