Providing PHP functions to call C# functions in Context

Integrating PHP and C# together. Using .NET libraries from PHP and vice versa.

Providing PHP functions to call C# functions in Context

Postby LInsoDeTeh » May 22nd, 2015, 10:07 am

Hi!
I have a problem getting started with Phalanger.

I've got a C# class called Mission, which loads a .php script into a ScriptContext when initialized. It's no problem now to call PHP functions from the C# class, but how can I provide functions from the C# class to the users, who write only PHP scripts, which get loaded by this class?

Code: Select all
public class Mission {
  private ScriptContext context; 

  public Mission() {
    context = ScriptContext.CurrentContext;
    context.Include("mission.php", true);

    var missionClass = (PhpObject)context.NewObject("Mission");
 
    var onLoadFunction = new PhpCallback(missionClass , "onLoadFunction");
    onLoadFunction.Invoke(null);
  }

  public void StartTimer() {
     //do something
  }
}




Now, in the mission.php I have the "onLoadFunction" function, which I want to call the "StartTimer" function of the Mission C# class.

Code: Select all
<?
class Mission {
  function onLoadFunction() {
    //do something and then
    StartTimer();
  }
}
?>


How can I achieve the function "StartTimer" is available in PHP, when the script is run by the Mission C# class itself?

I assumed I could achieve this goal with the context.DeclareFunction method, but somehow I failed to create a RoutineDelegate instance, which is one of the Parameters.
Another thought would be to pass "this" to any PHP function from the C# code, so all public functions of the Mission instance could be available in PHP via a variable pointing to the C# class instance.

Is one of the Solutions the way to go? Or how could I solve this problem?

Thanks so much in advance :-)
Cheers
Chris
LInsoDeTeh
 
Posts: 2
Joined: May 22nd, 2015, 9:53 am

Re: Providing PHP functions to call C# functions in Context

Postby LInsoDeTeh » May 22nd, 2015, 7:06 pm

Solved the problem myself. Thanks anyway.
LInsoDeTeh
 
Posts: 2
Joined: May 22nd, 2015, 9:53 am


Return to PHP/C# interoperability

Who is online

Users browsing this forum: No registered users and 1 guest

cron

User Control Panel

Login

Who is online

In total there is 1 user online :: 0 registered, 0 hidden and 1 guest (based on users active over the past 5 minutes)
Most users ever online was 151 on December 6th, 2020, 7:46 am

Users browsing this forum: No registered users and 1 guest