PHP 5.4 compiler for .NET/Mono frameworks. Phalanger compiles legacy PHP code to MSIL while being fully compatible with PHP behavior.

Project maintained by DEVSENSE Hosted on GitHub Pages — Theme by mattgraham

Note: There is a new, modern PHP compiler to .NET entitled Peachpie, which is being developed at the moment. Please see the Peachpie repository

By Jakub Misek, 12/05/2011

Phalanger 3.0 brings several cool and useful features, making interoperability between C# and PHP even easier. In this post, I will show one of them.

Use cases

Imagine several use cases.


Before Phalanger 3.0 release, you would need to write C# code in separated class library project, compile it into an assembly and add the reference into web.config file. This process is being used, however makes development a little slower than it has to be. Now this process is handled by ASP.NET and Phalanger altogether. You don’t have to configure anything and things do work as expected. Any public class, written in a script in a .NET language and placed in the App_Code folder is seamlessly visible in your PHP code, using standard PHP syntax. Demo

I’ve created simple demo here: App_Code interoperability. The demo consists of one PHP page, containing the presentation layer, using Class1 written in C#. Notice there is no configuration linking these two worlds, the things work as it would be expected in any other ASP.NET site.


The demo takes advantage of several features of Phalanger & ASP.NET.

<b>Camels created: </b><?= Class1::$CamelsProcessed ?><br/>
<b>Previous camel: </b><?= Class1::$LastCamel ?><br/>

    $x = new Class1;
    $growncamel = $x->MakeCamelFromGet(); // $x->Camel( $_GET['babycamel'] );
    if ($growncamel):
    ?><h2><?= $growncamel ?></h2><?

The code above shows simple usage, seamlessly using C# class in PHP code. It really does not seem to use any .NET class, however Class1 is written in C#. It is auto-loaded from App_Code folder.

How does it work

The App_Code thing takes advantage of ASP.NET which automatically pre-compiles all the code in special /App_Code folder. The resulting assembly is placed in the HttpRuntime.CodegenDir (Temporary ASP.NET Files). The assembly name can be found in XML file “App_Code.compiled“, in the XML element “preserve“, attribute “assembly” (if it is not configured differently).

Phalanger itself was able to reference .NET assemblies before. However now it loads the App_Code assembly automatically without a need of an other configuration and compilation.

The App_Code assembly is loaded in compile time. This results in several compile time optimizations, so using such classes is naturally fast. Also when changing the App_Code content Phalanger automatically recompiles PHP scripts.


In this post I tried to demonstrate how easily write a part of PHP web in C#. In this way you can integrate existing C# code base with PHP presentation layer, or just subsequently transform crucial parts of PHP code into C# classes. You can try the ASP.NET website I’ve prepared (App_Code interoperability). It needs just Phalanger 3.0 installed and ASP.NET web server (easiest way is to open Visual Studio 2010, hit File-Open-Web Site, and browse for the folder containing the demo. Hit F5 and run.