From 2f6a58cb1bcb4250652b54416ccf80946adb0c34 Mon Sep 17 00:00:00 2001 From: JasperVanEck Date: Mon, 2 Dec 2013 14:08:45 +0100 Subject: Updated README.md, implemented more complicated isCallable method and added comments to value.cpp --- Examples/CallPhpFunctions/callphpfunction.php | 23 +++++++++++++++++++---- Examples/README.md | 15 ++++++++++++++- 2 files changed, 33 insertions(+), 5 deletions(-) (limited to 'Examples') diff --git a/Examples/CallPhpFunctions/callphpfunction.php b/Examples/CallPhpFunctions/callphpfunction.php index ae3cf81..77d633c 100644 --- a/Examples/CallPhpFunctions/callphpfunction.php +++ b/Examples/CallPhpFunctions/callphpfunction.php @@ -6,13 +6,28 @@ * An example file to show the working of a php function call in C++. */ +class MyClass +{ + function method($a,$b,$c) + { + return $a+$b+$c; + } +} + +function myFunction($a,$b,$c) +{ + return $a+$b+$c; +} + /** * Call a C++ function with a callable PHP function as its parameter. * The PHP function is then executed from the C++ code. * The PHP function is this case, adds three numbers. */ -$result = call_php_function(function($a, $b, $c){ +echo(call_php_function(function($a, $b, $c){ return $a + $b + $c; - }); -// print the result -echo $result . "\n"; + })."\n"); + +echo(call_php_function("myFunction")."\n"); + +echo(call_php_function(array(new MyClass(), 'method'))."\n"); diff --git a/Examples/README.md b/Examples/README.md index ac66425..5bd9e1e 100644 --- a/Examples/README.md +++ b/Examples/README.md @@ -126,10 +126,23 @@ The following examples are available: implemented. It would need to be implemented for this specific example to work. - Functions and/or classes defined in this Example. + Functions and/or classes defined in this example. - void my_catch_exception_function(Php::Parameters ¶ms) - void my_throw_exception_function() +### [PHP function calls](https://github.com/EmielBruijntjes/PHP-CPP/tree/master/Examples/CallPhpFunctions) + + The seventh example shows how to pass a callable PHP function as + a parameter. As can be seen in the example, there are several ways + of passing a PHP function to the C++ function. When a function is + passed, it is possible to use the () operator on the parameter, with + the correct amount of parameters for the callable PHP function. + When using the wrong amount, or when trying to use the () operator + on a non-callable type, you will get PHP errors rather than + segmentation faults or other kinds of C++ errors. + + Functions and/or classes defined in this example. + - Php::Value call_php_function(Php::Parameters ¶ms) -- cgit v1.2.3