In our last blog post we discussed some of the advantages and shortcomings of PHP and the .NET framework, and pointed out why the two should not be compared with each other. Today, we are introducing a tool that can bridge the gap between these two frameworks, enable the developer to produce code in PHP that is both-way interoperable with .NET, and therefore make use of the specific advantages of both, PHP and the .NET framework: Peachpie, the PHP compiler to .NET.
What is Peachpie?
Peachpie is a modern PHP compiler based on Roslyn by Microsoft and drawing from our popular Phalanger project. It allows PHP to be executed within the .NET framework, thereby opening the door for PHP developers into the world of .NET and vice versa. Nevertheless, we must repeatedly stress that it is still a concept and work in progress.
The goal of the project is to compile legacy PHP code into portable class libraries, which enables developers to build cross-platform apps and libraries for Microsoft platforms quickly and easily. As a direct result, PHP applications powered by Peachpie would run across all devices and operating systems that are able to run .NET. Furthermore, Peachpie gives web-application developers the ability to deploy and run existing PHP code on an ASP.NET web server.
Besides granting PHP programmers access to cross-platform development, Peachpie allows for a full compatibility with .NET, which enables the development of hybrid applications, where part of the code is written in C# and part in PHP, or even the import of a .NET class into PHP scripts. The parts will be both-way interoperable and can communicate seamlessly, all within the .NET framework.
Finally, Peachpie allows for PHP applications to be developed directly in your favorite IDE, Microsoft Visual Studio. This way, the development experience is just like you are used to; PHP scripts are colorized, code errors are shown and the code can be compiled and debugged.
What’s in it for .NET developers?
The most significant benefits of the PHP programming language are its simplicity, ease-of-use, lightning fast project startup, a vibrant and enthusiastic community and the ubiquitous availability of extensions, libraries and various APIs. All of these features can be taken advantage of when developing a .NET-based project, since Peachpie enables the PHP code to be interoperable with the remainder of the project.
Furthermore, .NET programmers can benefit from the substantial amount of practical PHP functions and data structures, many of which are re-implemented in the managed environment of the .NET framework. The entire library of PHP functions and classes, including those implemented in the PHP extensions, suddenly becomes accessible to a .NET developer, along with the type information.
The Beginnings: Phalanger
Peachpie draws heavily from its predecessor version, Phalanger (PHP Language Compiler), whose beginnings date all the way back to 2005. Phalanger commenced as a project at the Charles University in Prague, received support from Microsoft, and even commercial support has been provided.
Unlike the modern Peachpie compiler, Phalanger is already a proven and working product. Multiple projects were based on Phalanger over the years; for instance, WordPress was enabled to run on .NET/Mono and Ajax-Zoom was able to create a powerful PHP/jQuery component enabling image zoom & pan gallery with a 360-degree rotation option for .NET using Phalanger. Multiple websites have been taking advantage of Phalanger, and numerous articles have been written about the project. You can check out all the references here or even visit the Phalanger GitHub to get a feel for how it works and try it out; it is a fully functioning compiler.
Transition Into Modern Times
So what has changed since the days of Phalanger? In short: Microsoft Roslyn. Roslyn is a .NET compiler platform, a set of open-source compilers and code analysis APIs for C# and VB.NET. Unlike traditional compilers, Roslyn helps developers to directly perform phases of compilation, such as lexical and syntactic structure analysis of source code, right from the onset.
Roslyn is a gamechanger in terms of the approach to the issue of Reflection. It provides an API for the serialization and de-serialization of metadata directly from the assembly, without the need to load it into the AppDomain. This not only speeds up the compilation process, but in case of dynamic languages such as PHP, it also makes the actual runtime faster. Furthermore, the compilation phases can be executed in parallel on multiplecore and manycore processors.
Roslyn compiler is able to work with portable libraries, which provides an outstanding framework for cross-platform projects and the compilation of cross-platform applications. Finally, Roslyn’s architecture allows for a greater variety of customization options for the compiled libraries, which is a massive benefit for non-standard languages like PHP, for which the C#-like API frequently is not enough.
In Part II, we will cover what’s new in Peachpie compared to Phalanger, and which benefits we are aiming for.