Lots of changes have been made to the Peachpie project this year already. So much so that we reached the next milestone in our roadmap and thus upped the version to 0.9. This article will explain what’s new and what’s still to come.
Peachpie is a modern second-generation compiler and runtime for PHP code under the .NET and .NET Core frameworks. It runs entirely in safe managed code and its objective is to be fully compatible with regular PHP, while offering a variety of advantages thanks to the .NET platform, such as performance enhancements or bidirectional interoperability between C# and PHP.
If this is your first time hearing of Peachpie, please refer to our Getting Started section to get going.
If you are already working with a Peachpie project, simply change the version of your referenced packages to
0.9.0-*. For more info on this, please see this article.
With Peachpie compiler, you can turn PHP code into EXE or DLL files that can be referenced from a C# project, for instance, or you can create a DLL, which you can deploy to a web server and maintain as a compiled sourceless website. Last but not least, Peachpie offers a plethora of security and performance benefits, as you can see in our benchmarks section. Check out our Getting Started section to see how to create a website.
What’s new in 0.9
This major update introduces several PHP constructs to our compiler that have been missing until now. Since version 0.9, it is possible to compile code containing traits, generators, catch blocks with multiple types specified, and catch blocks with an interface being specified. The runtime provides SHA512, PCRE with a \K option, tens of new standard functions and classes and more. Some of these features are not even possible in C#, so Peachpie is somewhat if a middleman between the PHP and .NET worlds, allowing you to hand pick exactly what you need from either platform.
Although essentially a syntactic sugar, it would still be great to have something similar to
yield from in C# as well. It offers the option of gradually returning elements of another
iterable object from a
generator function, and the compiler now supports this feature. It was one of the last constructs of the PHP language missing from the compiler before achieving a full compatibility with standard PHP.
yield from is much more powerful than it may seem. For example, if you are returning the values from another
generator function from it, the actual
yield from expression will ultimately only return its return value. With Peachpie 0.9, we are brining this and many other curiosities of the PHP implementation of this feature to the .NET environment.
Check out the following sample:
Implementing the trait mechanism in the .NET world was quite a challenge. There is nothing like it in C# or any other .NET language (and many would say for good reason). Traits also exist in Java, where they are actually well designed, unlike PHP, which implements traits very naively by simply copying the contents of the trait into the target class.
A trait is a construct similar to a class, whose internals are inserted into a class that claims to be using a trait. This allows one to achieve multiple inheritances and reuse code better.
See the following example of a trait:
The most interesting part of this mechanism is how the trait is compiled. The internals of the trait aren’t copied again and again each time the trait is used. The compiler internally creates an instance of a trait class (which can normally not be instantiated) in the class that is using the trait, generates method stubs that are in the traits and forwards the calls to the trait instance:
You can compare the PHP code to the resulting MSIL or to the decompiled C#.
Updated VSCode extension
As you may know, we have a free and user-friendly extension for Visual Studio Code, which enables you to develop with Peachpie in Microsoft’s wonderful lightweight IDE. The extension allows you to create new Peachpie projects seamlessly and utilizes the power of the Peachpie platform to you by providing syntax error underlining, diagnostics and tooltips. With the new version of Peachpie, we have also updated the extension to allow you to use all the new features.
Peachpie is edging ever closer to a fully functional version 1.0 with each commit. The project is now capable of running entire real world applications on .NET – among others we have previously talked about WordPress or older versions of CodeIgniter. As you will see very soon, however, we have added another major PHP application to the list: MediaWiki. We are currently finalizing a few minor fixes, but we are already able to run the entire project on Peachpie with just a few minor modifications.
Also coming up very soon is something that we are personally very excited about: a fantastic learning tool to help you visualize what exactly the compiler outputs live in real time. More on this next week. Stay tuned and follow our progress!