Scott Hanselman

Peachpie - Open Source PHP Compiler to .NET and WordPress under ASP.NET Core

July 27, '17 Comments [24] Posted in DotNetCore | Open Source | PHP
Sponsored By

The Peachpie PHP compiler project joined the .NET Foundation this week and I'm trying to get my head around it. PHP in .NET? PHP on .NET? Under .NET? What compiles to what? Why would I want this? How does it work? Does it feel awesome or does it feel gross?

image

Just drink this in.

C:\Users\scott\Desktop\peachcon> type program.php
<?php

function main()
{
echo "Hello .NET World!";
}

main();

C:\Users\scott\Desktop\peachcon> dotnet run
Hello .NET World!

Just like that. Starting from a .NET SDK (They say 1.1, although I used a 2.0 preview) you just add their templates

dotnet new -i Peachpie.Templates::*

Then dotnet new now shows a bunch of php options.

C:\Users\scott\Desktop\peachcon> dotnet new | find /i "php"
Peachpie console application peachpie-console PHP Console
Peachpie Class library peachpie-classlibrary PHP Library
Peachpie web application peachpie-web PHP Web/Empty

dotnet new peachpie-console for example, then dotnet restore and dotnet run. Boom.

NOTE: I did have to comment out his one line "<Import Project="$(CSharpDesignTimeTargetsPath)" />" in their project file that doesn't work at the command line. It's some hack they did to make things work in Visual Studio but I'm using VS Code. I'm sure it's an alpha-point-in-time thing.

It's really compiling PHP into .NET Intermediate Language!

PHP to .NET

You can see my string here:

Hello .NET World inside a PHP app inside the CLR

But...why? Here's what they say, and much of it makes sense to me.

  1. Performance: compiled code is fast and also optimized by the .NET Just-in-Time Compiler for your actual system. Additionally, the .NET performance profiler may be used to resolve bottlenecks.
  2. C# Extensibility: plugin functionality can be implemented in a separate C# project and/or PHP plugins may use .NET libraries.
  3. Sourceless distribution: after the compilation, most of the source files are not needed.
  4. Power of .NET: Peachpie allows the compiled WordPress clone to run in a .NET JIT'ted, secure and manageable environment, updated through windows update.
  5. No need to install PHP: Peachpie is a modern compiler platform and runtime distributed as a dependency to your .NET project. It is downloaded automatically on demand as a NuGet package or it can be even deployed standalone together with the compiled application as its library dependency.

PHP does have other VMs/Runtimes that are used (beyond just PHP.exe) but the idea that I could reuse code between PHP and C# is attractive, not to mention the "PHP as dependency" part. Imagine if I have an existing .NET shop or project and now I want to integrate something like WordPress?

PHP under ASP.NET Core

Their Web Sample is even MORE interesting, as they've implemented PHP as ASP.NET Middleware. Check this out. See where they pass in the PHP app as an assembly they compiled?

using Peachpie.Web;

namespace peachweb.Server
{
class Program
{
static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseUrls("http://*:5004/")
.UseStartup<Startup>()
.Build();

host.Run();
}
}

class Startup
{
public void ConfigureServices(IServiceCollection services)
{
// Adds a default in-memory implementation of IDistributedCache.
services.AddDistributedMemoryCache();

services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(30);
options.CookieHttpOnly = true;
});
}

public void Configure(IApplicationBuilder app)
{
app.UseSession();

app.UsePhp(new PhpRequestOptions(scriptAssemblyName: "peachweb"));
app.UseDefaultFiles();
app.UseStaticFiles();
}
}
}

Interesting, but it's still Hello World. Let's run WordPress under PeachPie (and hence, under .NET). I'll run MySQL in a local Docker container for simplicity:

docker run -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=wordpress -p 3306:3306 -d mysql

I downloaded WordPress from here (note they have the "app" bootstrapper" that hosts .NET and then runs WordPress) restore and run.

WordPress under .NET Core

It's early and it's alpha - so set your expectations appropriately - but it's surprisingly useful and appears to be under active development.

What do you think?

Be sure to explore their resources at http://www.peachpie.io/resources and watch their video of WordPress running on .NET. It's all Open Source, in the .NET Foundation, and the code is up at https://github.com/iolevel/ and you can get started here: http://www.peachpie.io/getstarted


Sponsor: Check out JetBrains Rider: a new cross-platform .NET IDE. Edit, refactor, test and debug ASP.NET, .NET Framework, .NET Core, Xamarin or Unity applications. Learn more and download a 30-day trial!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb
Friday, 28 July 2017 01:12:05 UTC
Pretty cool.
So, a way to run a php backend with more horsepower?
Fred
Friday, 28 July 2017 01:58:19 UTC
This is a huge development, PHP on .net would open up to hosting wordpress sites on windows based servers. I wonder how the performance will handle up
Friday, 28 July 2017 02:48:23 UTC
Joe, check out their benchmarks page
Friday, 28 July 2017 05:05:01 UTC
This is impressive amount of work, great naming and reuse of DLR (?!).

In my opinion, language interop between runtimes is more maintainable and leaner than re-implementation of the existing language on a completely different runtime. There will always be differences with the main implementation and incompatibility with existing libraries due to the dependence on runtime features such as GC, call stack, exceptions, etc.

But this is a nice marketing for PHP developers to get introduced into .NET ;)
Friday, 28 July 2017 07:42:17 UTC
When is the .net development team going to cherry pick the best PHP libraries, cross compile them to .net IL, build DLLs from that and include it in .net Core?

Maybe image processing, audio file metadata handling, video file metadata handling, ...

How's this different than IronPython and IronRuby which lasted 4 years (2006 - 2010) before being abandoned by MS?

Bob
Friday, 28 July 2017 08:58:14 UTC
When is the .net development team going to cherry pick the best PHP libraries, cross compile them to .net IL, build DLLs from that and include it in .net Core?
Maybe image processing, audio file metadata handling, video file metadata handling, ...
How's this different than IronPython and IronRuby which lasted 4 years (2006 - 2010) before being abandoned by MS?


Seeing the performance against plain PHP, I think this will last more.
Friday, 28 July 2017 13:03:47 UTC
impressive.. but still feels gross
st
Friday, 28 July 2017 13:56:55 UTC
There are other interesting PHP runtime alternatives as well, I'm especially intrigued by Tagua, whose lead developer is now employed by Automattic, the company behind WordPress.

More information here: Exotic PHP implementations: HippyVM, JPHP, Tagua VM, Peachpie
Friday, 28 July 2017 14:21:01 UTC
But why not just write your code in c#?
Friday, 28 July 2017 16:01:35 UTC
Next- Scala
WSL_YearOfLinuxOnDesktop
Friday, 28 July 2017 17:04:55 UTC
The language-as-middleware design reminds me of Giraffe for F# and ASP.NET Core
Monday, 31 July 2017 06:20:01 UTC
but still feels gross
Monday, 31 July 2017 06:20:32 UTC
ok
Monday, 31 July 2017 06:21:08 UTC
very good
Monday, 31 July 2017 09:34:30 UTC
This is a great post. So clear and easy to follow. Thanks for the tangible and attainable help. All your hard work is much appreciated. iPhone App Development Company Los Angeles
Monday, 31 July 2017 12:31:09 UTC
What Icon theme are you using?
Tuesday, 01 August 2017 19:35:16 UTC
The performance boost they are claiming is impressive. I would love to see moodle running on it.
Peter Thorpe
Thursday, 03 August 2017 02:14:56 UTC
Hi Scott,

Does it support version 2.0.0-preview2-006497 ?

I tried the following:

dotnet new -i Peachpie.Templates::*

dotnet new peachpie-console

Then when I ran dotnet restore, I got the following message:

c:\FirstPeachpie\FirstPeachpie.msbuildproj(22,3): error MSB4020: The value "" of the "Project" attribute in element <Import> is invalid.


I don't know if it has something to do with the dotnet version I'm using. But when I tried the same steps above with version 1.1, it worked fine.
Thursday, 03 August 2017 04:59:51 UTC
Get best online app to watch free movies onlie with following app apkdownloadapp.com/
Thursday, 03 August 2017 06:38:47 UTC
too many thanks
Friday, 04 August 2017 10:17:05 UTC
It's good information, thanks for share!.
Sunday, 06 August 2017 09:48:22 UTC
thanks for share this post
very good
طراحی سوپرمارکت
Sunday, 06 August 2017 09:51:27 UTC
Hi Scott,
Does it support version 2.0.0-preview2-006497 ?
I tried the following:
dotnet new -i Peachpie.Templates::*
dotnet new peachpie-console
Then when I ran dotnet restore, I got the following message:
c:\FirstPeachpie\FirstPeachpie.msbuildproj(22,3): error MSB4020: The value "" of the "Project" attribute in element <Import> is invalid.
I don't know if it has something to do with the dotnet version I'm using. But when I tried the same steps above with version 1.1, it worked fine.
قفسه فروشگاهی

وارد کننده ماشین آلات بسته بندی

تولید کننده کارتن
Friday, 11 August 2017 08:11:33 UTC
So,.net version of PHP ,wow , I found the new Vs tool that runs on Linux and Mac has lots of language plugin option, guys working on php also with it , but was not aware PHP compile to IL. Really cool .
Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.