Code Samples/Standard mode interoperability

From PhalangerWiki

Jump to: navigation, search

Using standard PHP library from C# and other .NET languages

If you have a bunch of working PHP code forming a library you use in your PHP applications and would like to use it in C# applications as well, you will find this tutorial useful. This tutorial demonstrates usage of Multi Script Assembly.

First, paste the following code into file called Library.php in some directory.

<?
  include "ClassC.php";
 
  function f()
  {
    echo "Hello!\n";
  }
 
  echo "Library initialized: Now, you can use ".
       "classes and functions declared here.\n";
?>

Then paste the following class in file named ClassC.php in the same directory:

<?
class C
{
  public $array = array(1,2,3);
		
  function __construct($data)
  {
    $this->data = $data;
  }
  static function foo(){ /* public static method */ }

  function bar(){ /* public instance method */ }
}
?>

To build the class library using Phalanger use following command:

phpc /target:dll /out:ClassLibrary.dll Library.php ClassC.php

This command builds the two scripts into an assembly called ClassLibrary. Lets create a C# console applications now that reference this library and call a function declared in Library.php and creates an instance of a class declared in ClassC.php.

To use PHP scripts library in your C# code, add following section into your configuration:

<phpNet>
  <scriptLibrary>
    <add url="ClassLibrary.dll" />
  </scriptLibrary>
</phpNet>

This will tell phalanger runtime where to look for php scripts.

In C#, the program using the library may look like following:

using PHP.Core;
using System;
 
namespace ConsoleApplication
{
  class Program
  {
    static void Main(string[] args)
    {
      ScriptContext context = ScriptContext.CurrentContext;
 
      // redirect PHP output to the console:
      context.Output = Console.Out; // Unicode text output
      context.OutputStream = Console.OpenStandardOutput(); // byte stream output

      // include the Library.php script, which initializes the 
      // whole library (it is also possible to include more 
      // scripts if necessary by repeating this call with various 
      // relative script paths):
      context.Include("Library.php", true);
 
      // call function f():
      context.Call("f");
 
      // create an instance of type C, passes array 
      // ("a" => 1, "b" => 2) as an argument to the C's ctor:
      var c = (PhpObject)context.NewObject("C", PhpArray.Keyed("a", 1, "b", 2));
 
      // var_dump the object:
      PhpVariable.Dump(c);

      // call static method
      var foo = new PhpCallback("C", "foo", context);
      var ret1 = foo.Invoke(null, new object[]{/*arg1, arg2, ...*/});

      // call instance method
      var bar = new PhpCallback(c, "bar"); // PhpCallback of instance method
      var ret2 = bar.Invoke(null, new object[]{/*arg1, arg2, ...*/});
    }
  }
}

You can use Visual Studio to build the C# program, or if you prefer command line compiler just run the following:

C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Csc.exe 
  /reference:"PhpNetCore, Version=3.0.0.0, Culture=neutral, PublicKeyToken=4ef6ed87c53048a3" 
  /out:ConsoleApplication.exe /target:exe
  Program.cs

And that's it ! If you run the resulting application you'll get:

Hello!
object(C)(2) { 
["array"] => array
{
  [0] => integer(1)
  [1] => integer(2)
  [2] => integer(3)
}
["data"] => array
{
  ['a'] => integer(1)
  ['b'] => integer(2)
}