One of the biggest issue for me in WinPHP challenge (more info) is using C# written libraries in PHP. Actual PHP part is very easy to write, because only thing you need to do is to create COM object. However, making your dll’s visible by PHP is a bit tricky.
I followed this tutorial, which has most of required information. Firstly, you create your “Class library”, then create assembly key file and assign it to project assembly. After project is built, you register and add to global assembly cache your library. Probably it’s quite natural process for all .NET developers, but for PHP developer like me, registering something with whole system seemed quite odd (dll hell?).
First problem was ComVisible. ComVisible is a keyword in assembly which is used to control types visibility: “If you need to access a type in this assembly from COM, set the ComVisible attribute to true on that type”. VisualStudio by default sets it to false, so all types are hidden and trying to register dll will result in this warning:
RegAsm : warning RA0000 : No types were registered
Luckily, I find out about ComVisible property quite soon – it’s quite hard to know about it if you haven’t done anything serious with .NET. Nevertheless, after setting it to true, everything should work fine. Sometimes VisualStudio (2008 Professional Edition) refused to build project because of locked dll, so don’t forget to try to kill PHP-CGI process first (Ctrl+Alt+Del). I didn’t had any other problems.
After some tests and failures I ended up with two simple C# classes and PHP caller. Class1 has two public variables (x, y) which are set by PHP in $math object. Method Class1.sum() creates new Math object with constructor parameters x and y, and calls getSum() which returns sum of x and y. Useless stuff, but it worked great to test if:
- COM objects works
- One class can call another (Class1 -> Math)
- Types are correctly understood/converted
Working PHP code (phpclass2 is C# library namespace):
$math = new COM ("phpclass2.Class1"); $math->x = 100; $math->y = 100; echo "sum = " . $math->sum(); unset($math);
Which successfully outputted:
sum = 200
I tried this code with the same Web Platform I used in Windows Server 2008, but now with older IIS (v5, Windows XP) and it worked perfectly with all default settings. Also, IIS control panel in Server 2008 is 10 times better than in Windws Xp – in Xp-one I couldn’t even find how to add new virtual host (my bad, but in IIS 7 it’s way easier to do). And because I was installing it second time, it took me only few minutes.
Getting this to work was probably one of the biggest milestones – actual frontend code and Deepzoom C# library is not that tricky. After some days of working with PHP+Windows things are starting to get a shape – except of different OS GUI, web server works good in both Linux and Windows – clearly Microsoft is holding this competition to show it. And they are in the right way.







