Click here to Skip to main content
13,357,610 members (33,955 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 6 Jun 2012

Passing .NET Delegates into PHP

, 6 Jun 2012
Rate this:
Please Sign up or sign in to vote.
How to pass .NET delegates into PHP

Phalanger is the only solution allowing to seamlessly interoperate between PHP and the .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: Action<int>((int i) =>
{ // do something
})); 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 ) {


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

The application lets the .NET code pass some delegates into PHP, and then it calls them to transform the 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 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.


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.


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


About the Author

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

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 6 Jun 2012
Article Copyright 2012 by Jakub Míšek
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid