Skip to content
Timm Friebe edited this page Jun 9, 2018 · 41 revisions

What is XP Compiler?

XP Compiler compiles future PHP to today's PHP. Kind of like the TypeScript of the PHP world.

It supports a large set of Hack, PHP 7.3, PHP 7.2, PHP 7.1 and PHP 7.0 but runs on anything >= PHP 5.6. 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

| Table of contents | | |-| | | Types | Trailing commas | | Annotations | | | Arrow functions | | | Compact functions | | | Constructor argument promotion | | | Using statement | | | Null-safe instance operator | |

Types

We allow you to express types more concisely. On top of primitives, void, arrays, callables, iterables and value types supported in today's PHP it allows function and union types, mixed, as well as array and map types:

function cookies(string $header): array<Cookie> {
  // …
}

On top of this, property type hints are supported:

class Person {
  private string $name;
}

Explore the type system.

Annotations

One of the most requested PHP features are annotations - or attributes. XP Compiler supports them using HHVM's attributes syntax:

class FixtureTest extends TestCase {

  <<test>>
  public function can_create() {
    new Fixture();
  }
}

Continue reading the overview of supported annotation features

Arrow functions

Using arrow notation, you can shorten closures:

$mul= function($a, $b) { return $a * $b; }
$mul= ($a, $b) ==> $a * $b;

All details on arrow functions can be found here.

Compact functions

Because the ==> operator essentially stands for omit braces and return, it will also work in function and method declarations:

class Person {
  private string $name;
  public function name(): string ==> $this->name;
}

This feature is unique to the XP Compiler and is described in the XP Compact functions RFC

Constructor argument promotion

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:

class Connection {
  public function __construct(private URL $url) { }
  public function url() ==> $this->url;
}

This feature comes from Hack language

Using statement

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= new Connection($url)) {
  …
} // $c->close() called automatically

See Using statement / Disposables for details.

Null-safe instance operator

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.

$name= $list->first()?->name() ?? '(no name)';

See also the Null-safe instance operator documentation.

Trailing commas

Trailing commas are allowed in all places PHP 7.3 supports them:

  • Arrays and maps - this has worked in PHP for a long time
  • Function calls - $f(1, 2, ), see this RFC
  • Grouped use statements - use lang\{Value, CommandLine, }, see here
Clone this wiki locally