Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Passing .NET delegates into PHP

, 5 Jun 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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 … Continue reading →

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:

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.

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

Demo

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

The application lets the .NET code to 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.

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 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 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 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)

Share

About the Author

Jakub Míšek
Software Developer DEVSENSE
Czech Republic Czech Republic
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.141022.2 | Last Updated 5 Jun 2012
Article Copyright 2012 by Jakub Míšek
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid