PeachPie- Run PHP on .Net Platforms

NEW CONCEPT

Using the PeachPie Framework.

What is PeachPie Framework?

PeachPie is a 2nd generation compiler and runtime of the PHP language under .NET Core.

It is a lightweight and astonishingly quick framework and most helpful of all: it’s cross-platform.

How PeachPie Works?

PeachPie Framework is a comprehensive re-implementation of the PHP language in C#, transforming PHP into a standard compiled language.

As an outcome, PeachPie permits PHP code to run on top of the .NET runtime and interoperate with .NET in both directions, taking benefit of all present .NET tools and solutions.

Why is that needed?

  • Re-using PHP components: PeachPie allows for all of these useful libraries to be re-used in .NET, instead of rewriting them.
  • Interoperability: PeachPie allows for .NET and PHP code to work side by side within one solution.
  • Migrating legacy apps: PeachPie suggests a convenient way of migrating from old PHP application to the modern .NET Core.

Check out the live compatibility status here

https://docs.peachpie.io/compatibility-status/

Prerequisites

It requires .NET Core SDK 3.0 or the latest. Link here.

Command Line Interface

Open a command prompt session and run the following command to fetch all PeachPie templates for .Net CLI:
dotnet new -i Peachpie.Templates::*

You can then create either a

  • Console app
  • Web app
  • Class library

Creating a console application using PeachPie

Open the command prompt, run the following command to create a console application similar to .Net Core

Notice an additional parameter-lang is used to specify the language used.

dotnet new console -o <ProjectName> -lang PHP

Project Structure

Consider the project name as “PeachPieExample.”

PeachPieExample
------- bin
------- obj
------- PeachPieExample.msbuildproj
------- program (PHP Source file)
------- Readme

Contents of program file

<?php
function main()
{
echo "PeachPieExample says \"Hello .NET World!\"";
}
main();

After creation, cd <ProjectName> and run below command

dotnet run

Console Output

PeachPieExample says "Hello .NET World!"

Github sample template

https://docs.peachpie.io/compatibility-status/


Creating a web application using PeachPie

Open the command prompt, run the following command to create a web application similar to .Net Core

Notice an additional parameter-lang is used to specify the language used.

dotnet new web -o <ProjectName> -lang PHP

Project Structure

Consider the project name as “PeachPieWebExample.”

PeachPieWebExample
------- Server
-------------- obj
-------------- bin
------- Website
-------------- Website.msbuildproj
-------------- index
------- Readme
------- PeachPieWebExample.sln

After creation, cd <ProjectName> and run below command

dotnet run -p Server

Browser Output

Browse localhost URL at port no 5004.

URL to browse
http://localhost:5004/
PeachPieWebExample says "Hello World!"

Github Sample

https://docs.peachpie.io/compatibility-status/


Creating a class library using PeachPie

Similarly, run the following in sequence to create & build a class library using PeachPie.

dotnet new classlib -o <ProjectName> -lang PHP
dotnet build

IDE Support

Projects can be created and opened in Visual Studio or Visual Studio Code:

Visual Studio:
1. Install PeachPie Visual Studio Extension.
2. Go to File | New | Project, locate the PHP (PeachPie) template group and create a new project.
3. Hit F5.
Visual Studio Code:
1. Install 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 tasks.json and launch.json files for .NET Core.
4. Edit launch.json to point to the actual build result.

Thank you for reading. I hope you like the article..!!

Leave a comment