Thursday, August 10, 2006

Running PHP on IIS with .NET framework

If you're thinking if it's possible to run PHP on IIS, then let me tell you that it's possible. But if you're wondering if it's possible to run PHP with ASP.NET, then I will recommend you Phalanger.

Phalanger, the PHP language compiler for .NET framework, enable the creation of module in enabling the execution of PHP scripts on the Microsoft .NET platform. This module is cooperating with the ASP.NET technology enabling it to generate web-pages written in PHP the same way ASP.NET pages are.

In contrast to the original PHP interpreter, our module is compiling the scripts into the MSIL (Microsoft Intermediate Language). While the first page access is delayed due to compilation, all following accesses benefit of the fast execution of the native code which is always far more effective than script interpretation. This gain is most important in case of common PHP-script libraries (for example PHPLIB, PEAR, Nuke etc.) which are complex yet immutable and thus their script files are compiled only once. On the other hand there may be some code in the PHP scripts which is not known in the time of compilation and has to be compiled at run-time. The .NET platform fully supports run-time code generation. Although this procedure handicaps script compilation a little, it is known that PHP constructs imposing run-time compilation are used rather rarely and if, there is not much code to deal with.

The principal goal of our project is to enable full functionality of existing PHP scripts without any modification. The only condition is that these scripts do rely neither on special functionality provided by the UNIX platform or the Apache server nor on undocumented, obsoleted or even faulty functionality of the PHP interpreter. The built-in PHP functions provided to programmer by the PHP interpreter (for example string and array manipulation etc.) have to be reimplemented to reach this goal. They are rewritten in the managed .NET environment, namely in C#, and thus many of these functions will be reusable by other .NET application programmers.

The Phalanger compiler will compile the PHP language into the MSIL byte-code. It is thus the front-end of the compilation while the back-end is provided by the JITter (Just-In-Time compiler) which is a part of the .NET framework. That's why we do not address the native code generation nor optimization. Our task is to compile the PHP scripts into .NET assemblies - logical units containing MSIL code and meta-data.

To be able to reuse compiled code as much as possible, it is necessary to compile individual PHP scripts separately. This is in contrast to the behavior of PHP interpreter: when a PHP script references another (for example using the include construct), the interpreter performs actions very similar to actual source code copy-paste inclusion at that place. This means that code sharing involves lot of code reinterpretation unless it is cached and optimized by some additional third party tool. That's why the Phalanger compiler compiles individual files to unilaterally independent modules (an included script is not dependent on the including one) to enable linking of the resulting modules without the need of recompilation. This approach requires careful treatment of the compiled form of scripts which may or may not enable such optimization.

I have yet to try it out myself but I'm certainly be interested to folk some time out to play it. I probably will blog about it sooner or later once I get some time for it. Go download Phalanger and have fun PHP'ing while ASP'ing~!

Your Ad Here