It’s been a long time coming, but after extensive stabilization work and implementing as many missing functions as was humanly possible, we finally got the PeachPie platform to a state where we can comfortably consider it usable.
PeachPie is a second-generation compiler and runtime of the PHP language under .NET Core. We started developing it in 2016 after years of working on the first generation of the compiler, called Phalanger. There are several reasons why we chose to start over instead of continuing with Phalanger, but the main ones boil down to two major breakthroughs Microsoft offered: Roslyn and .NET Core.
Roslyn offered a standardized and centralized compiler API that solved many of Phalanger issues and allows any project built on top of it to work perfectly with other codebases that utilize Roslyn. .NET Core, on the other hand, is a lightweight and incredibly fast framework and best of all: it’s cross-platform.
How PeachPie works
PeachPie is a complete re-implementation of the PHP language in C#, turning PHP into a standard compiled language. PHP projects are analyzed and built, spitting out a standard managed DLL file, PDB, XML documentation and even a NuGet package if you wish – just like any other .NET language would.
As a result, PeachPie enables PHP code to run on top of the .NET runtime and interoperate with all .NET languages in both directions, taking advantage of all existing .NET tools and solutions.
We know you’d ask. So here’s why:
- Re-using components from PHP: you might not be a fan of PHP, but there’s no denying the language’s popularity. After all, 80% of websites are powered by it and the amount of PHP code out there is staggering. With this overwhelming market share, a vast majority of companies have some kind of library, application or website, which they rely on, in PHP. PeachPie allows for all of these useful libraries to be re-used in .NET, instead of rewriting them.
- Interoperability: going hand in hand with the previous point, PeachPie allows for .NET and PHP code to work side by side within one solution, with absolutely no overhead, marshalling, or dirty hacks. PHP projects can seamlessly reference other .NET projects or even NuGet packages.
- Migrating legacy apps: many of our customers have older PHP applications (we’ve seen PHP 3.x in production), and sometimes it’s hard to update, let alone re-write, a mission-critical app in C#. PeachPie offers a comfortable way of moving from legacy PHP to the modern .NET Core step by step. You can replace parts of your app with C# and gradually migrate.
- Performance: the idea is that compiled code simply runs faster. This used to be extremely true in the days of PHP 5, but nowadays, PHP is far from the performance bottleneck it used to be, especially with the JIT plans for PHP 8. Nevertheless, ASP.NET Core, in combination with the Kestrel web server, is one of the absolute fastest setups in the world. We continuously implement improvements to PeachPie’s performance and the ultimate goal is definitely to run PHP applications faster than the PHP runtime can.
- Security: making changes to a PHP site live on the server and instantly seeing the result may be one of the key reasons the language became so popular. However, it also opens up these sites to a whole plethora of security exploits. We’re by no means claiming that PeachPie makes websites impenetrable, but the beauty of compiled sites is that no code that wasn’t compiled with the original application will be executed on the server. In addition to that, the code is obfuscated, making it even more secure, and entire PHP apps can be delivered as source-less NuGet packages.
Back in 2017, PeachPie was one of the first projects outside of Microsoft to join the .NET Foundation, a non-profit organization to support the open-source .NET ecosystem. Check out the full article that talks about joining the .NET Foundation and what this means for the project.
PeachPie has been open source and under the liberal Apache 2.0 license from the get-go and it always will be. The community has provided us with a huge amount of support over the years, with 18 contributors outside of the core team and countless issues filed by the community. We’re incredibly thankful for all these contributions.
Compatibility with PHP
A bulk of our work on PeachPie at this point boils down to two activities: fixing bugs and improving the compatibility with PHP. At the end of the day, everything works exactly as it does in PHP, but using completely different mechanics. PeachPie provides a complete runtime replacement instead of the interpreter and a replacement for all the native extensions. Since it compiles PHP to Common Intermediate Language bytecode, all the language features are handled by the .NET runtime. As a result, the compiled application runs completely differently with the same results, taking advantage of the .NET Just-In-Time compiler and offering type safety.
PHP, on the other hand, has an undeniably amazing variety of useful projects. Say what you will, WordPress is still by far the #1 CMS in the world with its vast plugin ecosystem, but there is a plethora of other applications and tools worth looking into.
Currently, PeachPie supports a number of real-world applications and a majority of apps should already run or be very close to running on .NET. We’ve supported WordPress for many years (check out our very recent video on the Channel 9 show On .NET) and we recently found many others to run on PeachPie already:
- Twig – the popular PHP templating engine
- Symfony – a massive PHP framework
- CodeIgniter – another popular PHP framework
- MediaWiki – a gigantic application that powers Wikipedia
- WooCommerce – the number one e-commerce platform, which is also a plugin for WordPress
- Laravel – possibly the most popular PHP framework of them all, should be fairly close to running on PeachPie
PeachPie is actually a massive project. We always have to balance between being fully compatible and supporting every possible edge case and making sure our performance doesn’t suffer. We’ve been stabilizing PeachPie as much as we possibly could and since compiling PHP into statically typed byte code is still a fairly new topic, we have experimented with various new approaches. We’ve been playing around and we invented an improved inter-procedural type analysis, code flow transformations, dynamic methods invocation, runtime type information for PHP, runtime-compiled overload resolution, mixed data structures, abstracting host environments, various seamless interoperability options, compiled PCRE RegEx processing, debugging eval’ed code, and much more. We always measure the performance impact of our implementations and compared it with other solutions. This gave us valuable insights, which will be used to maybe redesign some of the used approaches in the next big version.
With all the latest testing and fixes, and with several very large applications running on PeachPie in production, we’re comfortable enough with releasing a version of PeachPie that starts with a 1 🙂
What you get with PeachPie
Aside from all the aforementioned advantages of running PHP on .NET, you also get a bunch of amazing side benefits we essentially inherit for free from the .NET ecosystem.
First and foremost, you get a whole different developer experience. You can debug your PHP code in Visual Studio or VS Code using the .NET debugger and .NET profiler, you can change the current statement position, inspect variables, use the exception helper, check the debug log in the output debug window, or even debug remotely. You’ll get all kinds of highlighting and diagnostics directly in your code during design time.
PeachPie is partially supported in JetBrains Rider as well. We’re currently waiting for a few minor issue fixes and we’ll likely fully support this new IDE soon.
Of course, you also have the option of using the command line using the
dotnet command to create, build, run, trace, pack, etc.
Finally, there is also the option of publishing any PHP app we compile as a source-less NuGet package, which allows you to provide entire applications, components, or libraries without giving away their actual source code. If you want to find out more, check out this tutorial.
The main prerequisite is to install .NET Core SDK 3.0 or newer. If you don’t have it already, grab it here. You can then proceed either on the command line or in a development environment, such as Visual Studio or Visual Studio Code.
Open a command prompt and run the following command:
dotnet new -i Peachpie.Templates::*
You can then create either a console app, a web app, or a class library:
Creating and running a console project:
dotnet new console -lang PHP
Creating and running a web application:
dotnet new web -lang PHP
dotnet run -p Server
Creating and building a class library project:
dotnet new classlib -lang PHP
Projects can be created and opened by .NET development environments, such as Visual Studio or Visual Studio Code:
1. Install our PeachPie Visual Studio Extension.
2. Go to
File | New | Project, locate the
PHP (PeachPie) template group and create a new project.
Visual Studio Code:
1. Install our PeachPie for VS Code Extension.
2. Open the folder with your PHP project.
3. Start the project by pressing
F5 and let VSCode create the initial
launch.json files for .NET Core.
launch.json to point to the actual build result.
Over the next couple of months, we want to keep improving the user-friendliness of PeachPie (e.g. by improving our error messages and documentation), but we also need to perform a few compile-time and runtime optimizations to enhance our performance. Aside from that, we also want to allow for an
include of generated scripts, implement
dynamic support in C# and more C# interoperability features, and standardize our API.