PHP Sharp

By | | PHP

Be warned. Reading this may give you cause to choke on your turkey leftovers.

A couple of years ago, Alan Knowles did some experimenting with getting PHP to run under .NET. He documented his expierences here. Alan managed to put together a working PHP compiler and reached the “Hello World” stage, I believe. Of course there’s a long way to go from Hello World to something that’s actually useful, such as how to implement PHP’s function library. Things that make you think only the insane would want to attempt.

Well it turns out a group of Czech programmers were insane enough to do it – meet Phalanger, a PHP compiler for .NET.

From a little messing around, the basics work. Here’s the output from phpinfo();

They also seem to have implemented some of the better known PHP extensions like GD and PDF. From the Overview.

The principal goal of our project is to enable full functionality of existing PHP scripts without any modification.

Not sure how I feel about Phalanger yet. Two key phrases from the license, which stuck out;

You may use this Software for any non-commercial purpose

You may not reverse engineer, decompile, or disassemble the Software

Also failed to find any mention of Mono. Given that the source is unavailable, running Phalanger under Mono on *Nix is not an option right now.

At the same time, having access to the .NET library including those ASP.NET controls is a tempting prospect…

Anyway, something to experiment with.

Written By:

Harry Fuecks

Harry has been working in corporate IT since 1994, with everything from start-ups to Fortune 100 companies. Outside of office hours he runs phpPatterns: a site dedicated to software design with PHP that aims to raise standards of PHP development. He also maintains Dynamically Typed: SitePoint's PHP blog.

 

{ 28 comments }

scania August 6, 2008 at 1:43 pm

tcFnEQ skf83dnbfv9h2v64rn0vm

Anonymous March 9, 2006 at 11:55 am

Remember the “Clipper” compiler for dBASE III?

Have a look at and

QJustQ December 4, 2005 at 10:21 pm

OK, so I just want to create stand-alone exe’s running from the command-line using PHP5 classes and extensions and not have to distribute my source code.

In this case, a compiler to exe is what I want.

Remember the “Clipper” compiler for dBASE III?

Brayan May 17, 2005 at 11:55 am

Come on PHP.NET is great. If you don’t like it – don’t use it. I like it I use it :P

Devicemangler April 15, 2005 at 5:41 pm

I just started using C# and have been having trouble finding any modules or extensions that will let me use PHP with C# instead of ASP. any pointers would be appreciated

been January 8, 2005 at 11:22 pm

If I can believe my school papers on Analysis and Design, strong typing means that type errors are completely excluded from occurring at runtime, which seems to make any compiled language where you can unconditionally cast an object, weakly typed.
Eiffel for example is strongly typed (or so those papers say), VB.Net is not (One can mis-DirectCast() an element from a collection class instance for example), I have no experience with C#.Net but I’m guessing the same ‘issue’ exists.

HarryF January 6, 2005 at 7:35 pm

Dynamically typed? I think a requirement for .NET language is its being strong typed.

It’s not a requirement. It’s just the only successful (in terms of performance) languages so far have used strong typing – IronPython seems to be breaking the mold, using techniques / optimizations no one had tried before.

Mike Borozdin January 6, 2005 at 9:57 am

[quote=HarryF]Meanwhile .NET doesn’t, yet, have a stable dynamicically typed language available[/quote]

Dynamically typed? I think a requirement for .NET language is its being strong typed.

Anonymous January 5, 2005 at 4:04 pm

oups, sorry :S

Tomas Matousek January 3, 2005 at 9:11 am

We hadn’t developed a new language. There are no major modifications to PHP 5 grammar and functionality in Phalanger.

Anonymous January 2, 2005 at 12:56 pm

i don’t get it. why .net?? why do people always have to make new stuff?? just like microsoft, you want us to waste our time learning a new language just for the sake of it??

okanturk December 30, 2004 at 10:16 am

It is a shame how people become disrespectful for others protecting their own work and say do not steal. I am not talking about Harry, but not everybody has to give away their work, get over with. If you are looking for free stuff, that’s fine, but bashing others for protecting their own work is evil and gives us a big clue about your character and quality of your own work.

Tomas Matousek December 29, 2004 at 3:07 pm

Hi!
As one of the authors of Phalanger, I would like to post several comments here.

1) We would like to support Mono, but there are some problems with it. Firstly, I wonder if there exists a quality MC++ compiler for Mono. The Phalanger is written in C# and MC++. Particulary, the parser, generated by Bison, and the Extension Manager enabling calls from .NET to original PHP extension libraries are written in MC++. Moreover, the Extension Manager is tightly bound to Windows platform so it would be necessary to rewrite it for Linux and that is not so trivial.

2) The “edit-save-reload” technique still works with Phalanger. ASP.NET server watches changes being made to scripts on the server and if something changes it removes respecite compiled assemblies from its cache and recompiles the sources on the next request. There are two ways how script inclusions can be handled. Included scripts can be linked either statically or dynamically depending on the configuration in web.config file(s) and arguments passed to include* constructs. The former method is faster in run-time but causes more recompilations if sources are modified. Hence it is useful when the application has already been debugged. The latter one makes no dependencies between scripts so changes to includee doesn’t cause a recompilation of the includer. This is useful when sources are changed frequently.

3) Some notes about our licensing policy. Phalanger is free for users who would like to try it and/or use it on their personal web pages or to write console applications for their personal usage in it. It is not available for commercial purposes for now. However, this is not definite and in the future Phalanger will probably be released also for commercial applications. The source code of the Phalanger Class Library is available since the Class Library is extensible and a user can write his/her own functions (e.g. in C# or VB.NET) and make it available for PHP language. The user can learn how to do that from the Class Library manual and the Class Library source files. on the other
hand, the source code of the Phalanger Core and its developer documentation is not avaiable for public.

Manolo G December 29, 2004 at 1:37 am

err, what? php for .NET? Ok it sounds..hmm..interesting.

Recently I got myself an MCP for .NET but my everyday work is in PHP. I agree with the comments that say C# is the right tool for the job in .NET but try to look it from another perspective. We have great software writen for PHP, and the .NET framework is very powerful, please trust me on this one, so I think that if there is a *clean* way to make both work together in an application, I vote for it.

I think PHP is reaching an age where it no longer needs to be a web application only scripting language, but some popular and powerful tool to build complex solutions and perhaps projects like Phalanger are in the right direction. Hell, even Sun has plans to support PHP as a first line scripting language for Java.

I don’t like the licensing approach for Phalanger though.

HarryF December 28, 2004 at 11:29 am

It’s beyond me why developers waste their time in such ways? .NET is a great framework, but just learn C# or VB.NET and be done with it!

Really you have to look to some of the re-implementations of Python to see the potential impact of something like PHP#.

Jython is popular enough to have had a book written about it while Iron Python was enough to get MS to hire it’s developer.

That’s not to say vast numbers of developers have switched from the original CPython but Jython and IronPython provide further options for non-standard use cases (e.g. a bank which requires all code run under the JRE).

The normal PHP distribution has a massive installed base for a server platform and that’s never going to be swept away overnight.

Meanwhile .NET doesn’t, yet, have a stable dynamicically typed language available (JScript isn’t it; IronPython may well be). Whatever the rights and wrongs, there’s a user base that demands PHP.

Brego December 28, 2004 at 10:39 am

I simply cannot understand why they are wasting their time? Instead of helping php-team out, they play with some money-centred technology… Shame.

cory December 28, 2004 at 8:43 am

It’s beyond me why developers waste their time in such ways? .NET is a great framework, but just learn C# or VB.NET and be done with it!

The whole thing reminds me of a book title I read about 5 years ago: “ASP 3.0 with PERL for Microsoft Windows”… very silly.

Anonymous December 27, 2004 at 4:06 pm

why do people lose their time trying to port php to something else?
I mean, the zend engine sux, why don’t they try to enhance/fix it ?

sde December 27, 2004 at 10:14 am

err.. no thanks. c# is the best tool for the job with .net

i’ve coded with avr.net ( rpg for .net ) ( only cause i had too ). if you’re going to have to learn to code .net with php, just learn c#. going from php to c# is not that difficult.

HarryF December 27, 2004 at 7:59 am

The function library is the easy part — you just do it one at a time, implementing according to the documentation.

Guess so. Considering the existing PHP function library, implemented in C, plus extensions (and the tersely documented Zend API), reimplementing in C#, where memory is managed, is no doubt a breeze by comparison.

Other issues are more difficult, like reimplementing the OO quirks, making the basic data structures perform well, etc. Once you’ve implemented the language properly, it’s “only” a matter of putting in time to implement the library.

There’s some things like includes inside functions which I’ve yet to test.

Also haven’t checked how well (performance wise) it handles dynamic types – believe Microsoft’s JScript implementation does a relatively poor job there (they seem to encourage you to use static typing). Believe (not sure) IronPython was the first to show what can be done, by virtue of code generation ‘n stuff.

Thanks for the link to IronPHP BTW – wasn’t aware of that.

HarryF December 27, 2004 at 7:45 am

and certainly a worthy exercise … but why PHP.NET? Sort of defeats the advantages PHP has in speedy development and ease of use!

From the little messing around I did, it felt like PHP i.e. save file > reload page > *TADA*.

That said some of the larger samples provided with Phalanger took quite some time to compile. Once compiled, not problem but if you’re making incremental changes in an include file in a large PHP app, this might get frustrating – haven’t got that far yet though – just hacked up a few basic scripts.

Dangermouse December 27, 2004 at 6:55 am

evil, evil and evil.

Ian Bicking December 27, 2004 at 4:34 am

The function library is the easy part — you just do it one at a time, implementing according to the documentation. Other issues are more difficult, like reimplementing the OO quirks, making the basic data structures perform well, etc. Once you’ve implemented the language properly, it’s “only” a matter of putting in time to implement the library.

JNKlein December 27, 2004 at 3:36 am

Interesting concept … and certainly a worthy exercise … but why PHP.NET? Sort of defeats the advantages PHP has in speedy development and ease of use!

agile December 26, 2004 at 11:01 pm

The work news coming out of ‘IronPHP’ looks interesting as well, re: http://numbers.wordnerd.org/blog/

yousf December 26, 2004 at 8:14 pm

hey man!
who wants this UGLY .NET frame work!
I really like my Apache personal server, which run on my XP windows.

MiiJaySung December 26, 2004 at 6:11 pm

Despite the closed source system that is around now, I’m sure it won’t be long before someone looks into getting it to work with Mono now it’s been mentioned in the Public domain.

Anyway, it seems interesting, along with the possible plans that PHP might also get ported to work on the Parrot VM. With any luck, us PHP’ers will have the choice between the two maybe in another 18 months ;)

M36Teen December 26, 2004 at 2:14 pm

*choke* Uhh *cough* we uhh *keels over* I’ll have to get back with on this a little later – after I finish choking!

Comments on this entry are closed.

{ 1 trackback }