top of page

How can PHP be converted to Java?

In general, there is not much about PHP that can't be converted to Java, though it is not particularly easy to do. What needs to be done is to convert the code and include a support library to match the features that aren't available in the target language.

Each modern language has its own set of built in functions. In PHP, these consist of the standard library functions, as well as a number of popular extensions. In Java, that consists of various built in packages. For a converter to work, it needs to support all of those built in functions.

An interesting fact of modern languages is that they are often written in two languages, one of which is usually C/C++. PHP's standard functions and extensions are written entirely in C. This makes it very easy to call them from another language with a small wrapper library. Arguments and return variables are copied before and after the function call. This is admittely a large overhead, (especially on Java JNI), but these function calls can later be re-written. The important thing is to keep the application's structure intact so that development can continue with working software.

Secondly, there are some aspects of a language like PHP which are really functions. They appear as syntax, but really they are anything but simple operations. A good example of this is the PHP comparison operators. There is a c function in the PHP codebase that handles these comparisons at runtime, doing several levels of type conversion. In Java, the equivelent of that would not be to convert "<" operator into "<", but to make a special function call.

The PHP language is full of many language features that in other languages are represented as function calls. For example, the $_GET array and other super-globabl variables. In PHP these are real arrays, but their equivelant in Java is represented by calling ->getParameter() and various other functions. To replicate this behavior in Java, there is a need for a helper library to populate these arrays.

This kind of "full conversion" where the application ends in a working state is often called a migration. It is not a new idea, but there isn't anything like it available for PHP. Facebook developed a source code converter from PHP into C++ that ran eventually their entire platform, but they had no intention of ever writing code in C++. It was just a quick way for them to make use of the g++ compiler's optimizations. The code that is produced was hideous (http://stackoverflow.com/questions/2611516/what-does-the-c-output-of-the-hiphop-php-compiler-look-like), designed only to be optimized by the compiler. A real migration would intent to put the code in a readable state. There is no reason why it can't be done, but it is too large an undertaking to be worthwhile for most companies to write their own converter and helper library just to convert their own application(s).

Tags:

Featured Posts
Recent Posts
Archive
Search By Tags
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square
bottom of page