# JPHP

Designed by Dmitry Zaitsev 2014 Apache License 2.0 http://j-php.net/

JPHP is a new implementation for PHP which uses the Java VM. It supports many features of the PHP language (5.6+).

How does it work? JPHP is a compiler like javac, it compiles php sources to JVM bytecode and then can execute the result on the Java VM.

## Goals

JPHP is not a replacement for the Zend PHP engine or Facebook HHVM. We don’t plan to implement the zend runtime libraries (e.g. Curl, PRCE, etc.) for JPHP.

Project started in October 2013. There were a few reasons for that:

1. Ability to use java libraries in PHP
2. Upgrading performance via JIT and JVM
3. Replacing the ugly runtime library of Zend PHP with a better runtime library.
4. Using the PHP language not only on the web
5. Also: unicode for strings and threads

== Technical details == The whole language is written from scratch in Java using the ASM library to generate bytecode, it uses all the popular jvm languages, such as Groovy. As the build system was chosen Gradle.

To my surprise, the Java technology stack provides a very comfortable environment for writing jvm language. No need to write your VM with JIT, garbage collector and class system have been implemented without a headache for cross-platform, and the jvm bytecode is very easy to understand. However, there were many pitfalls and, most of all because of the magic of language php.

JIT possible to increase productivity by 1-10 times, depending on the test, on average 1.5-3 times to the real code.

## Compatible with PHP?

It is necessary to distinguish between language and library to it, so the compatibility level languages ​​and libraries are two different things. At the beginning of the development, I realized that to write compatibility with all due PHP library can not be one person. I decided to focus only on the language, though implemented basic things such as spl autoloading, Reflection, ob_ * function <? ...?> And more.

JPHP takes about 300+ unit tests from the original Zend PHP, including the testing of the PLO, functions, operators, etc. There are also tests its own. This helps to make sure that the language is working properly.

• The language at the level of PHP 5.3 (except for the goto)
• Type Hinting for callable (5.4)
• Short syntax for arrays (5.4)
• Class :: {expr} (), (new Foo) -> bar () (5.4)
• try ... finally (5.5)
• Array and string literal dereferencing (5.5)
• Function array dereferencing foo () [0] (5.4)
• System constant class name for the class (5.5)

## JIT and performance

What do you think, which code can affect performance? If you are familiar with Facebook HHVM, then I think you know what. The main problem of PHP performance is a global space for variables, variables magic just magic when you can access the variable name at runtime. For this reason, different JPHP can compile the same code. Where there is no magic to the variables, the compiler converts the indices and variables at runtime immediately turn to them by index.

$var = 'foobar';$x = 'var';
${$x} = 'chanched'; // variable magic
$global_var = 100500; include 'file.php'; // variable magic, в include necessary to transfer global scope with names function foobar() {$x = 100500;
$var = get_defined_vars(); // variable magic } Therefore, when it is expected to appeal the variable name at runtime, JPHP table retains variable names, and when not - does not save and access the variables directly in the indices. Improperly selected variables about 2 times slows down your code JPHP. The Zend PHP code works the same in all conditions. ## Optimizer PHP optimizer knows quite a lot, here is a small list of its features. ### Constant expressions $x = (20 + 30) . 'foobar';

</syntaxhighlight>

### Static constants

There are some constants, which PHP knows at compile time, and there is the dynamic constants declared by define. Static system is constant __FILE__, __DIR__, __CLASS__, constant java extensions, constants that are declared in a single file through a const. All of them can be replaced during the compilation:

include __DIR__ . '/core.php'; // конкатенация произойдет во время компиляции

### Immutable functions and methods

These are the functions and methods, the result of which is unchanging, or that can be calculated at compile time once, they do not affect the global environment and, therefore, it is safe.

for ($i = 0;$i < 100500; $i++){$x = cos(1) + 3; // посчитается 1 раз во время компиляции, а не 100500
}

In this example, the function cos () system and immutable, and the parameter passed to the function is constant, so the result is cos (1) will never change.

To apply immutable functions / methods that are declared by the programmer, who do not have parameters and return a constant expression. Call this function JPHP comparable in speed to a constant conversion, for example:
function getVersion(){
return '1.0';
}$x = getVersion(); // быстрый вызовdefine('VERSION', '1.0');$x = VERSION; // стоимость вызова константы = стоимости вызова предыдущей функции

### Impossible conditions

If an if or elseif you constant expression that is just false, the compiler will throw quite superfluous condition. While it supported only if, else, and elseif. For example, it can be very helpful:

if (TRACE_ENABLED){ // если TRACE_ENABLED = false, то компилятор сможет удалить мертвый код
log("Log text...");
}

## Caching classes, functions, byte code?

The model works JPHP allows you to store the compiled code in memory, ie, classes and functions. Once compiled and loaded class will be used repeatedly. In the future, it will write the http server frameworks for performance that rivals Phalcon at the PHP. These easy to store between requests and the http server, you can write to the php, as I will explain below.

In JPHP a special class of Environment, which lets you create isolated environments for scripts execution, it is something like a sandbox expansion of runkit. Each such surroundings its own set of classes, functions, and global variables.

• Environment - an isolated environment for script execution
• Similar to the sandbox of runkit