Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / PHP

Passing .NET Delegates into PHP

0.00/5 (No votes)
10 Jun 2012CPOL2 min read 7.6K  
How to pass .NET delegates into PHP

Phalanger is the only solution allowing to seamlessly interoperate between PHP and .NET world. It allows to use e.g. .NET generics, namespaces, properties, method overloads, global variables, constants and .NET delegates in both worlds. In this post, I will show how to pass delegates from C# into PHP, and how to use them in PHP.

This interoperability feature is available since Phalanger 3.0, and it is very useful, e.g., when writing C# plugins for PHP systems.

Imagine you have PHP function foo, getting a callable argument. Now you can call such function from C#, and pass a delegate as you are used to:

PHP
x.foo(new Action<int>((int i) =>
{ // do something
}));
x.bar(new Func<int, bool>(this.someMethod));

The conversion to PHP callable function accepts any .NET MulticastDelegate. In this way, you can extend functionality of PHP application by passing .NET methods inside. PHP code itself does not need to be changed. It uses .NET delegates as usual PHP callable object; it can be called, copied or passed as callback to any function.

PHP
function foo( $func ) {
    $func(123);
}

Demo

To demonstrate the feature, we have a simple demo called ClrDelegates.

Image 1

The application lets the .NET code pass some delegates into PHP, and then it calls them to transform given text. It makes use of App_Code feature and other interoperability features that will be described in some future article.

PHP
foreach ($callbacks as $fnc)
     $str = $fnc($str);

The usage from within PHP code is very natural, as you can see above.

How Does It Work

When a MulticastDelegate object is passed from .NET to PHP world, Phalanger wraps the delegate into PHP object with __invoke() method. The __invoke() method is a natural way of creating callable PHP objects. Inside the function, it simply invokes the original MulticastDelegate with given arguments. Thanks to this mechanism, PHP code does not even notice, it uses .NET delegates. You can only notice it by checking the class’s name.

Phalanger also checks if you are passing the right amount of arguments. Note the types are not implicitly converted yet. .NET may throw an exception if argument’s type does not match the delegate’s type.

Conclusion

Phalanger 3.0 offers an easy and useful way of injecting .NET method callbacks into PHP. It is natural to use from within C# and PHP too. Download and try the ClrDelegates to see how seamlessly it works.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)