Maintaining backward compatible Peachpie and PHP code simultaneously

When you consider transforming your PHP code to .NET using Peachpie, you’ll probably worry about backward compatibility and the development process itself, or whether you can maintain both platforms within a single code base. Here is why you don’t have to.


If you are not familiar with the project and have some concerns, Peachpie takes regular PHP source code and compiles it. Similarly to the C# compiler, it produces valid, safe DLL or EXE files that run on top of the .NET/.NETCore/Mono runtimes. It does not (and cannot) depend on PHP or any of its native extensions at all. The reason for this is to manage the security of the code, increase performance, integrate with other .NET (mostly C#) languages and take advantage of industry standard modern tooling designed for the .NET platform, including CPU and memory profilers, obfuscators, IDEs, cloud services and others.

Peachpie doesn’t break the rules

The goal of the Peachpie project is to process legacy PHP code with little or no modifications, and to generate a .NET assembly that behaves the same way as it does in PHP. Although there are intentional differences, there is no additional syntax and it is always possible to design the code in a way that runs in the same fashion on regular PHP and on .NET.

It’s our main objective to be fully compatible with regular PHP, so that entire applications and frameworks will be able to run on Peachpie with the least possible amount of code modifications. Developers and especially companies mostly worry about the difficulty of managing the transition from PHP to Peachpie and they sometimes mistakenly believe that they have to flip the switch and hope for everything to work. We would like to debunk this myth in this article.

Maintain code for PHP and .NET jointly

The beauty of Peachpie is that you can actually make the transition gradually and test each compiled component thoroughly before running it in production. When migrating to .NET, you just keep the PHP project as it is and you may continue with the development as you are used to. Additionally, by adding the MSBuild project file, you just taught the code to be compiled into .NET.

The MSBuild file is the industry standard project file describing the compilation, post build tasks, deployment etc. – all well understood by Visual Studio or command line utilities. This has no side effects to the original code and allows your development team to seamlessly switch between PHP and .NET. Once the code works well under .NET, you can keep using .NET only or continue maintaining the code for both platforms. It’s all up to you and your requirements.

If you decide to drop the support for PHP altogether and keep using only .NET, you can take advantage of many features the project provides. Removing parts of the PHP code and rewriting it to a C# project is my favorite one.

Check the platform in runtime

There are surely situations when you’d like to check whether the code runs on regular PHP or on Peachpie. For this purpose, we have an elegant and standard solution – when running on Peachpie, the runtime implicitly defines a global constant PEACHPIE_VERSION. If you ever used HHVM by Facebook, you may have seen something similar with their HHVM_VERSION constant. The code below demonstrates this feature nicely:

This ties in nicely with the compiler! The condition is actually evaluated in compile-time and has no performance overhead during runtime. The code above is reduced by the compiler and the if condition is removed completely.


This is actually important to know: the PHP code can target both the PHP and .NET runtimes at once. When transforming an existing code base to .NET, first it can be tested thoroughly and slightly modified to run smoothly on .NET as it is, all while still being maintained and running on PHP in production. Only when properly tested and when we are sure we want to, we can switch the platform to .NET and for example continuously rewrite routines and classes to C#.

When compared to the standard process of completely rewriting an application, this gives you a lot more flexibility and agility in your development and allows you to streamline your processes much more precisely.

Posted on January 18, 2018, in category Information, tags: , , , , , , , ,