GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.php-cpp.com — PHP-CPP - A C++ library for developing PHP extensions DocumentationFound a typo?You can find this documentation page on GitHub. Lambda functionsC++ and PHP both support lambda functions or anonymous functions (in the C++ world the word 'lambda' is most used, PHP people speak about 'anonymous functions'). With PHP-CPP you can pass these functions from one language to the other. It is possible to call an anonymous PHP function from your C++ code, and the other way around, to call a C++ lambda from a PHP script. Calling anonymous PHP functions from C++Let's start with a very simple example in PHP. In PHP you can create anonymous functions, and assign them to a variable (or pass them directly to a function).
The code above should be familiar to most PHP programmers. The 'other_function' can of course be implemented in PHP user space, but to demonstrate how to do this with PHP-CPP we are going to build it with C++. Just like all the other functions that you've seen in the earlier examples, such a C++ function function receives a Php::Parameters object as its parameter, which is a std::vector of Php::Value objects.
It is that simple. But the other way around is possible too. Imagine we have a function in PHP user space code that accepts a callback function. The following function is a simple version of the PHP array_map() function:
Imagine that we want to call this PHP function from your C++ code, using a C++ lambda function as a callback. This is possible, and easy:
In the example we assigned a C++ lambda function to a Php::Function object. The Php::Function class is derived from the Php::Value class. The only difference between a Php::Value and a Php::Function is that the constructor of Php::Function accepts a function. Despite that difference, both classes are completely identical. In fact, we would have preferred to make it possible to assign C++ functions directly to Php::Value objects, and skip the Php::Function constructor, but that is impossible because of calling ambiguities. The Php::Function class can be used as if it is a normal Php::Value object: you can assign it to other Php::Value objects, and you can use it as a parameter when you call user space PHP functions. In the above example we do exactly that: we call the user space my_iterate() function with our own 'multiply_by_two' C++ function. Signature of the C++ functionYou can pass different sort of C++ functions to the Php::Function constructor, as long as they are compatible with the following two function signatures:
Internally, the Php::Function class uses a C++ std::function object to store the function, so anything that can be stored in such a std::function object, can be assigned to the Php::Function class. Found a typo?You can find this documentation page on GitHub. Who is behind PHP-CPP?PHP-CPP is made by Copernica, a leading provider of high quality marketing software located in Amsterdam, The Netherlands. With Copernica you can deliver relevant and timely communications using email, sms, landing pages and PDF. More from CopernicaContact usStay up to date |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |