BANGALORE, INDIA: The .NET development framework facilitates writing applications in more than one programming language like VB or C#. While writing a .NET application, you can always opt for a language of our choice. You can even write each module (say a Windows form or a Web form) of an application in a different language.
With a piece of software/plugin called Phalanger, you can use PHP as one of the languages for writing .Net applications. Plus these applications, written in PHP will be compiled – as with any application written in .NET language. Plus, Phalanger offers an extension/plugin for Visual Studio which allows you to write PHP based .Net applications in Visual Studio 2008 (the latest in the Visual Studio family).
Get, set and go...
Assuming that you have already installed and are running .NET 2.0 (or above) framework and Visual Studio 2008, you require to do the following to download and install Phalanger:
1. Download phalanger-2.0-march-2008.zip from the URL http://www.codeplex.com/Phalanger/Release/ProjectReleases.aspx?ReleaseId=11564#ReleaseFiles.
2. Unzip the file and run the installer via setup.exe. The installer also sets up IIS web server for PHP (to be precise, for PHP Phalanger).
3. To write PHP Phalanger applications in Visual Studio, you need to install the Phalanger extension which is a separate download.
4. Go to the above mentioned link and download the file vsintegration-2.0-march-2008.zip. Unzip the file and run vsiip.msi.
Configure IIS Web Server
To configure IIS on Windows XP to use Phalanger, execute the following steps:
1. Goto Control Panel>Administrative Tools>Internet Information Services.
2.Expand the tree (in the left pane) till you see “Default Web Site”. Right click on it and select Properties.
3.Click on the “Home Directory” tab and then click on the Configuration button. Under the Mappings tab, click on Add. Type in the following for the Executable and the Extension:
Executable: C:\WINDOWS\ Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll
Extension: .php
4. Leave the other settings to their default. Restart IIS.
Besides web applications, you can write assembly/class libraries, console applications and Windows forms based applications using Visual Studio as the RAD tool and PHP as the language. |
Hello Phalanger
Let us write a simple Hello Phalanger web page. Type in the following code in a text editor (like Notepad):
print(“Hello Phalanger”);
?>
Save the above file as hello.php in c:\inetpub\wwwroot. Browse to the web page at http://localhost/hello.php and you will be greeted by a “Hello Phalanger” message. Note that we did not install PHP (from www.php.net) or PHP ISAPI DLL or PHP CGI. Phalanger has taken over as the scripting engine for .php files. It compiles and displays the output of the PHP code.
With the Phalanger extension for Visual Studio, you can write the above mentioned Phalanger page in Visual Studio. Now we need to start Visual Studio 2008. For this, click on File->New Website. Select PHP from the language dropdown and then select Phalanger PHP Web. This will open a new Visual Studio project which will include a file named default.php. Type in the above print( ) statement in default.php.
Right click on the Website in the Solution Explorer and select “View in browser”. This will launch Visual Studio's built in web server and will show a page with the files. Click on default.php to see the output.
Debugging Phalanger apps in Visual Studio
Before you debug Phalanger apps, you need to take care of the following. In the debug mode, Visual Studio does not show the runtime values of PHP variables on mouse over. Instead you will need to use the Locals window.
To inspect the runtime values of PHP variables, you will need to launch the Locals window, while in debug mode, via Debug>Windows>Locals. In this window, you can spot the local (to a function) variables. To look at global variables, expand the
PHP Assembly in C# applications
After the Hello Phalanger program, lets look at something more interesting. What if one can write assemblies (or class libraries) in PHP which in turn can be used in .Net applications (Web or Desktop)? This will prove useful if you have an existing investment done in a complicated logic written in PHP and now want to use the same logic in a .Net application.
As a simple example, we write a PHP class library named Cryto which has a function/method called GetMD5(). This function simply accepts a string, uses PHP's inbuilt MD5 function and returns the MD5 hash of the string.
In Visual Studio, click on File>New Project. Under the “Project Types” menu, select Phalanger. From the right pane, select Phalanger Class Library. For the project name, type in Crypto and then click on Ok button. Now go to the the Solutions Explorer and here click on the Library.php, which will look like this:
class Library
{
function __construct()
{
}
}
You need to modify the code to the following:
{
function __construct()
{
}
function GetMD5($str) {
return md5($str);
}
}
?>
Now build the project and create a new .Net Web application in C#. Right click on the Project and click on Add Reference. Then browse to the assembly file (Crypto.dll) of Crypto. The assembly file can be found, typically, in My Documents\Visual Studio 2008\Projects\Crypto\Crypto\bin\Debug. You will also need to add a reference to PhpNetCore.dll (found in Program Files\Phalanger\Bin directory).
Now in the code behind (Default.aspx.cs, for example), you can call the the GetMD5() function, of the assembly written in PHP. This can be done as shown below:
Crypto crypto = new Crypto();
string str = (string)crypto.GetMD5("Hello Phalanger");
Response.Write(str);
To explore more about Phalanger plug-in, browse to the site http://php-compiler.net/doku.php and learn more about other nifty uses that you can put this software for.