What’s new in Peachpie 0.8.0

Many new features have been implemented into Peachpie lately and a lot has happened around the project. It’s time to raise the version to 0.8.0 and discuss what’s new in it.

Content

  1. New features
  2. GitHub issues opened
  3. Interoperability enhancements
  4. Other news

Newly supported PHP features

There has been quite a bit of movement in the Peachpie Compiler Platform – thousands of lines of code have been written, Peachpie has been extended with hundreds of new functionalities, and there has been plenty of other news as well. First of all, let’s take a look at what newly implemented features are included in Peachpie 0.8.0:

  • Catch for interfaces and classes that aren’t resolved at compile-time. This is a particularly useful feature, as there is no efficient way of doing this in C#; Peachpie solves this issue by using a catch filter.
  • Argument unpacking: a highly useful feature that allows programmers to easily pass arguments from an array to a function call.
  • PHP 5.4 static lambdas and lambdas with implicit $this
  • An alpha version of the PHP graphics library
  • Hundreds of resolved issues and implemented operators
  • Countless library functions, classes and operators added to Peachpie, such as:
    1. AppendIterator, LimitIterator, SplObjectStorage, ReflectionClass, ReflectionMethod, and dozens more
    2. iconv, iterator_count, iterator_to_array, iterator_apply, spl_object_hash, class_implements, and a plethora of others

These advancements and newly added features mean that Peachpie is edging closer towards a full compatibility with the PHP language. To get a better overview of just how close we are and what’s missing, you can either check out our Roadmap or the Compatibility Matrix.

Opened Issues

Starting with Peachpie 0.8.0, we have finally enabled the posting of issues on GitHub. While we definitely encourage everyone to file them whenever they come across any problems, we do ask you to be patient with how fast we get to fixing them. We are aware of plenty of issues at this point and will get to them throughout.

Interoperability Enhancements

We have been discussing various specific problems with people on our Gitter chat and implementing fixes for them to be able to advance with their particular project. What’s great about this is that people continuously find new ways of interoperating between C# and PHP and mutually using objects from each respective language. The more of these uses we know of, the more we can improve Peachpie, so we really encourage everyone to experiment.

C# foreach on PHP values

Starting now, it is straightforward to enumerate values originating from PHP code using the C# foreach. Peachpie implements GetEnumerator implicitly and provides an enumerator wrapper that C# understands. This allows one to enumerate PHP iterator objects and even PHP generators.

PHP

C#

[csharp]
using (var phpctx = Context.CreateEmpty()) {
var c = new PhpClass(phpctx); // create PHP class

foreach (var x in c.x) { // enumerate PHP iterator
Console.Write(x.Value.ToString());
}

foreach (var x in c.y) { // enumerate PHP object
Console.Write(x.Value.ToString());
}

foreach (var x in c.g()) { // enumerate PHP generator
Console.Write(x.Value.ToString());
}
}
[/csharp]

PHP array access of .NET arrays

Simple scenarios, such as passing objects from C# to PHP, can now be enhanced by passing .NET IList (and Arrays) and seamlessly use its items in PHP. The PHP (Peachpie) runtime understands the IList interface and provides the corresponding access. The code in PHP looks like an access to PHP arrays or objects implementing ArrayAccess, but it also consumes IList without any other code needed.

C#

[csharp]
static void Main(string[] args) {
using (var phpctx = Context.CreateConsole()) {
// `CreateConsole` implements PHP’s `echo` as `Console.Write`
PhpClass.printme(phpctx, PhpValue.FromClass(new string[] { "Hello", "from", "C#" }));
PhpClass.printme(phpctx, PhpValue.FromClass(new List<int>(){ 9, 8, 7, 6 }));
// `PhpValue.FromClass` won’t be necessary in future updates
// for now it ensures the developer is aware of the casting
}
}
[/csharp]

PHP

Standard ASP.NET integration

This allows you to build PHP websites in .NET on ASP.NET/IIS really easily with no C# code behind it.
We already have a sample on how to build an ASP.NET Core WebSite in PHP (http://www.peachpie.io/getstarted). Its only drawback for me personally is that I have to code the web server and configure it by myself programatically. Sometimes I prefer the “old” way: the IIS Management Console and ASP.NET. Here, I can configure the entire web server in a `web.config` file in a declarative way and thus keep my website’s code clean. This is how the PHP website running on IIS+ASP.NET Integrated Pipeline would look like:

`Web.Config`:
[xml]
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="PhpHandler" path="*.php" verb="*" type="Peachpie.RequestHandler.RequestHandler,Peachpie.RequestHandler" />
</handlers>
</system.webServer>
</configuration>
[/xml]

`phpweb.msbuildproj`:
[xml]
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup Label="Globals">
<ProjectGuid>0e51d101-0992-4aa6-a134-26ea3f2e3934</ProjectGuid>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net46</TargetFramework>
<OutDir>.bin</OutDir>
</PropertyGroup>
<ItemGroup>
<Compile Include="**/*.php" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Peachpie.Compiler.Tools" Version="0.8.0-*" />
<PackageReference Include="Peachpie.NET.Sdk" Version="0.8.0-*" PrivateAssets="Build" />
<PackageReference Include="Peachpie.RequestHandler" Version="0.8.0-*" />
</ItemGroup>
</Project>
[/xml]
`Build the website`:
[shell]
dotnet restore
dotnet build
[/shell]

The above procedure results in two things: all the PHP code in the project directory is compiled into bin/phpweb.dll and Peachpie.RequestHandler is set to be used as a handler for `*.php` files. The handler understands the compiled PHP code and responds by executing the appropriate script from the compiled assembly phpweb.dll.

Casting Between .NET and PHP Types and More

More interoperability features are coming. We have already added some implicit cast operators, but this will change in the future. The goal is to make PHP code, despite the fact that it is so different, cooperate with .NET straightforwardly, with all the dynamic values converted seamlessly.

Note that we plan to support IDynamicMetaObjectProvider, as well. However, since the PHP runtime is based on an excessive use of value types to optimize the use of the Garbage Collector and memory, we are postponing the implementation of this interface for future work.

How to update

To update to the newest version of Peachpie in your existing projects, just specify the version as 0.8.0-* in your project file and call `dotnet restore`:
[shell]
dotnet restore
[/shell]

Other News

In case you missed it, Peachpie has joined the .NET Foundation last week. Check out our article to understand what that means for the project or read the official announcement by Jon Galloway.

The legendary Scott Hanselman has also written a fantastic article about Peachpie on his blog and Microsoft MVP Gunnar Peipman has also written a tremendous tutorial on how to use Peachpie to run PHP applications on .NET Core. Both articles are definitely worth checking out.

We will write up a detailed blog post just about this topic, but it’s worth mentioning that we successfully managed to compile and run PHPUnit. We will use this in the future to prove the correctness of Peachpie-compiled applications.

Finally, we have also launched a brand new website for Peachpiestudio.com, where you will find commercial tooling and services around the Peachpie Compiler Platform in the coming months. Currently, we are keeping our VS Code extension there and we are working hard on an integration into the full Visual Studio IDE. Stay tuned for more news in this regard.

Posted on August 9, 2017, in category Announcement, Information, News, tags: , , , , , , ,