Run PHP on .NET with PeachPie

.NET is much more than Microsoft’s own tools and languages. The open source PeachPie brings PHP to the table.

Run PHP on .NET with PeachPie

One of the more interesting things about .NET is its extensibility. It’s not just a Microsoft-only development platform with Windows-focused frameworks and languages. Even in the early days of the platform, tools such as IronRuby and IronPython brought popular open source languages to the CLR, while projects such as Unity were built on top of .NET, adding additional libraries and services.

At the heart of .NET is the open source Common Language Runtime, CoreCLR. This is a combination of an architecture-specific just-in-time compiler, RyuJIT, and an interpreter for the project’s CIL (Common Intermediate Language). Backed by open standards, .NET languages compile to CIL, with the CLR handling run-time compilation. There’s even the option of compiling straight to architecture-specific binaries built around CoreRT, a native runtime environment.

The standards-based environment makes it relatively easy for third parties to bring their own languages and environments to .NET, supporting everything from 3D gaming with Unity to PeachPie’s implementation of the familiar PHP web development environment.

Introducing PeachPie

PeachPie is particularly interesting, as PHP remains a popular way to quickly build and deliver web applications. Intended to be language-compatible with the standard PHP release, it lifts and shifts PHP code to ASP.NET Core environments such as Azure, compiling PHP code to .NET-native CIL rather than using the standard PHP tools. Instead of running multiple environments, you’re running one, but you can choose different development tools to build and deploy pages and applications.

You’re able to integrate the two technologies, implementing ASP.NET C# alongside PeachPie PHP in the same project. Work done to build sites in technologies such as Blazor can be called directly from PHP and vice versa. Existing PHP libraries and functions are visible to your C# code, and you can call them using familiar constructs. A commercial support option from the development team means you’re not on your own or struggling to get community support.

This makes tools like PeachPie particularly useful: You can bring existing resources and applications to a new platform without making major changes while your developers get up to speed with new technologies. It’s an interesting look at how architectural decisions made for a language that’s targeted at its own interpreter need to be translated to a whole new ecosystem. As it’s an open source project with an Apache 2.0 license, you’re able to look at the code and send in pull requests.

Translating low-level concepts can be hard, especially without direct mapping between the two ways of working. In the PeachPie blog, the design team shows how they have managed the transition between a relatively basic concept in PHP (declaring an explicit interface) and the resulting CIL code. There’s also the issue of going from a loosely typed environment like PHP to one that’s strongly typed by default.

In practice of course, much of the PHP functionality can be handled by library functions. PHP’s approach to implementing operations as functions simplifies the tasks PeachPie runs, as a PHP function can be mapped directly to a .NET library. All that’s necessary is for PeachPie to act as a black box, call a .NET function when a PHP equivalent is expected, and return the right outputs for the current inputs. It can similarly map UI constructions, like grids and forms, to the equivalent ASP.NET controls.

Building code with PeachPie

Getting started with PeachPie is quick; it installs as a Visual Studio extension from the Visual Studio Marketplace, adding a set of templates to the New Project dialog. You can use this to start building a PHP-enabled ASP.NET application or a new library so your existing back-end code can be called from PHP. Once you open a new project, PeachPie will download its dependencies, and you can start coding. As you’re using Visual Studio, you’ve got access to all its tools, including IntelliSense and the built-in debugger. PeachPie compiles sites and runs them just like you run ASP.NET, so you can use existing .NET debugging skills and tools from F5 and services to work with remote sites on Azure or your own web servers. As an added bonus, there’s support for using Azure DevOps, ensuring your sites are part of your CI/CD (continuous integration and continuous delivery) pipeline.

Modern .NET development doesn’t need Visual Studio, so you can work with PeachPie directly from the command line using the .NET SDK. Download the PeachPie templates from the dotnet CLI app: dotnet new -i Peachpie.Templates::* This installs the templates to build ASP.NET apps, class libraries, and console applications. Interestingly, there’s even the option to use PHP to build Blazor WebAssembly apps and PHP WebAssembly apps, allowing your users to run PHP code at native speeds in their browsers.

You can now start building a PHP site using dotnet new web -lang PHP. This creates two directories: Website, with your site code in it, and Server, which is the entry point of the web app. To launch your site, simply change the directory to the Server directory and type dotnet run. You can then navigate to localhost and the port detailed in the ASP.NET command line view.

This approach lets you choose your web development tool to edit your site code. The initial index.php file contains a simple PHP application to display some text in a browser so you can test for basic PeachPie operation. Any PHP code ported to your site can be copied here for testing before being uploaded to your ASP.NET host.

Using .NET to take PHP beyond the web

As PeachPie makes PHP a .NET language, you’re not actually limited to building web apps. It’s now possible to take your PHP skills and use them to build stand-alone .NET code, for example, using it in console applications. PHP’s declarative programming approach can help bridge the gap between low-code development and large-scale applications, allowing quick PHP code to be used as application modules and libraries.

There’s a lot to like about PeachPie. Making PHP interoperable with .NET is a worthy goal. A lot of PHP code is still in use, and a lot of capability in modern ASP.NET can help power that PHP code while still allowing .NET developers to add deep enterprise integrations into existing PHP front ends by building C# functions and calling them from PHP. Mixing and matching tools like this gives PHP developers a path to ASP.NET development and its Razor inline code options—an effective hybrid development model.

The result is a tool that shows off the capabilities of the .NET platform, going beyond Microsoft’s own languages and tools. At the same time, architects and development managers can maximize available resources on a single platform without requiring expensive and time-consuming training. Old code gets to run on new systems, and new code gets to build on decades of PHP experience. It’s a win for everyone.

Copyright © 2022 IDG Communications, Inc.