You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Timm Friebe edited this page Mar 23, 2024
·
41 revisions
What is XP Compiler?
XP Compiler compiles future PHP to today's PHP. Kind of like the Babel of the PHP world.
It supports a large set of Hack, PHP 8.3, PHP 8.2, PHP 8.1 and PHP 8.0 but runs on anything >= PHP 7.4. Builtin features from newer PHP versions are translated to work with the currently executing runtime if necessary.
XP Compiler can be used in two ways: First of all, there is a command line tool, xp compile. Second, it plugs into the class loading mechanism and compiles code just in time - providing a seamless experience of writing code, saving, and running PHP.
Features
Some features from newer PHP versions as well as Hack language are still missing. The goal, however, is to have all features implemented - with the exception of where Hack's direction conflicts with PHP! An overview can be seen on this Wiki page.
We allow you to express types more concisely. On top of primitives, void, arrays, callables, iterables, objects and value types supported in today's PHP it allows nullable, function and union types, mixed, as well as array and map types:
Another way to shorten your code is to use constructor argument promotion. It replaces the declaration of a member and its initialization via the constructor with a single keyword:
Another HHVM / Hack feature supported by XP Compiler is the using statement. You might know this from C#, or as "try with resources" from Java. It integrates with both the Disposabe interface as well as XP's lang.Closeable.
using ($c= newConnection($url)) {
…
} // $c->close() called automatically
Null handling can be tedious. XP Compiler supports the ?-> operator, which accesses instance members like ->, but only if the expression on the left-hand side is not null.
Because of the way the XP Compiler is implemented, dereferencing from an instance creation expression can be done without the need to put it inside braces:
$string= newDate()->toString();
Of course, surrounding it with braces also works!
Multiple catches
Sometimes, it is necessary to handle unrelated exceptions in the same manner:
This PHP 7.1 feature is simulated on PHP 7.0. In addition, XP compiler also supports omitting the types in order to catch all exceptions (try { … } catch ($e) { … }).
Constant modifiers
Class constants can have a public, private or protected modifier since PHP 7.1:
With XP Compiler, throw can be used inside ternary (?:) and null-coalesce (??) operators as well as inside lambda expressions and compact function syntax.
$command= $args ? $args[0] : thrownewIllegalArgumentException('Argument expected');
class Test {
publicfn invoke() => thrownewMethodNotImplementedException(__METHOD__);
}
This feature is unique to XP Compiler and neither present in PHP nor HHVM. It is inspired by C# 7.0 and an open JavaScript proposal, as well as Kotlin.
Splat operator in arrays
Instead of having to write array_merge(), the XP Compiler will allow using the ... operator inside arrays:
$commandLine= ['xp', Splat::class, ...$args];
// same as:$commandLine= array_merge(['xp', Splat::class], $args);