Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling F# (.Net dll) code from php

Tags:

.net

php

dll

f#

com

I was wondering if it was possible to call some F# code from php. It seems like Phalanger does the trick. Is anybody using it ? For this solutions and others( if there are any?), what requirements does it have on the server to run the code ?

Thank you

like image 325
jlezard Avatar asked Dec 28 '22 08:12

jlezard


2 Answers

Yes you can, by using PHP COM class but it works only on Windows version of PHP5+ and needs no separate installation. So, you do like this:

<?php
$obj = new COM("myfsharp.dll");

$output=$obj->HelloWorld(); // Call the "HelloWorld()" method from the DLL.
// once we created the COM object this can be used like any other php classes.
echo $output;
?>

And if you're skeptical about using PHP COM class, then read this from the PHP Manual:

Starting with PHP 5, this extension (and this documentation) was rewritten from scratch and much of the old confusing and bogus cruft has be removed. Additionally, we support the instantiation and creation of .Net assemblies using the COM interoperability layer provided by Microsoft.

This article shows all the changes done.

like image 186
shamittomar Avatar answered Dec 30 '22 20:12

shamittomar


Phalanger should be able to call F# code directly (just like any other compiled .NET code. The code would be the same as when working with standard PHP types (although Phalanger has extensions that allow you to use .NET specific things like generics - which could be tricky using PHPCOM).

The requirements to run F# code from PHP site compiled using Phalanger are just .NET 2.0+ and IIS (to host the web site). For F#, you would need to reference your F# library and FSharp.Core.dll (which contains F# runtime and basic types).

PS: I was involved with Phalanger for some time, so if you have questions, let me know - I can forward them to the current developers (they can also provide some commercial support if you were interested).

like image 29
Tomas Petricek Avatar answered Dec 30 '22 20:12

Tomas Petricek