What’s New in PeachPie 0.9.990

For the end of June, we prepared some final cleanups and fixes, improved APIs, and also a bunch of new features. The compiler now handles the majority of publicly available opensource code with no or very few modifications. Also, there are some PHP 8 features available if you’re keen to try them in your code.

Compiler Updates

The most significant update is the implementation of previously unsupported PHP constructs. Namely, it was the array function declaration (very similar to C#’s lambda). Since 0.9.990, the following code compiles and runs correctly:

<?php
$y = 1;
$fn1 = fn($x) => $x + $y;
echo $fn1(2);

In addition, a finally block can contain a return statement and generates correct IL. This is done by generating a state machine around try/catch scopes. The following PHP code generates the IL below. Notice the actual return if emitted outside the TryCatchFinally scope.

function f() {
   try {
   }
   finally {
     return 1;
   }
 }
 IL_0000: nop
 IL_0001: leave.s IL_000a
 IL_0003: rethrow
 IL_0005: stloc.1
 IL_0006: ldc.i4.2
 IL_0007: stloc.0
 IL_0008: leave.s IL_000c
 IL_000a: br.s IL_000c
 IL_000c: ldc.i4.1
 IL_000d: conv.i8
 IL_000e: ret
 Try IL_0001-IL_0001 Catch class [Peachpie.Runtime]Pchp.Core.ScriptDiedException IL_0003-IL_0003
 Try IL_0001-IL_0001 Catch class [mscorlib]System.Exception IL_0005-IL_0008

Another nice feature is the resolution of include expressions at compile time. As a result, some file inclusions may be compiled into static method code instead of requiring a runtime hashtable lookup and full path resolution.

 include __DIR__ . "/wp-header.php"; // compiled into the most efficient form 

Base Library

PHP consists of a huge set of built-in functions, classes, and constants. For the most part, they are wrappers around popular C libraries. PeachPie is fully managed, and in order to make PHP applications run on top of .NET, those libraries have to be reimplemented in C#, either using existing opensource projects or by providing our own solutions.

This update includes a few more functions, improving the overall compatibility with PHP’s BCL. Most notably, we now support a bunch of image* functions, and updated the PCRE functionality.

ASP.NET Core integration

The API for the integration with ASP.NET Core was improved, and an option to set options was added.

public void ConfigureServices(IServiceCollection services) {
  // a new API allowing you to set PHP options in standard way
  services.AddPhp( options => {
    // e.g.
    options.Session.AutoStart = true;
  });
public void Configure(IApplicationBuilder app) {
  /* add PHP to your request pipeline,
   * - PHP assemblies do not have to be specified anymore
   * - the middleware probes your app dependencies and finds PHP assemblies with PHP scripts. Scripts will be then served upon HTTP request
   */
  app.UsePhp();

For more information, see the documentation page at https://docs.peachpie.io/net/hosting/aspnetcore/ .

PHP 8.0 features

PHP version 8 is around the corner, and there are a few RFCs already accepted with neat features. PeachPie already provides most of the features, including extended syntax, new constructor property promotion, throw expressions, or non-capturing catches. The following code now compiles and runs on PeachPie:

<?php

class X {
  function __construct(public $p, ) {
    try {
      echo $p ?? throw new OutOfRangeException;
    }
    catch (Exception) {
      echo "some err";
    }
  }
}

$x = new X("Hello");
echo $x->p;

Conclusion

PeachPie 0.9.990 should be the last major release before version 1.0, as the platform now supports a majority of major PHP frameworks, applications and packages. We wanted to iron out a few details and offer basic support for the upcoming PHP 8 version before launching 1.0, which we expect to do in the coming weeks.

Posted on June 21, 2020, in category Announcement, Information, News, tags: , , , ,