diff options
-rw-r--r-- | Examples/CallPhpFunctions/callphpfunction.php | 23 | ||||
-rw-r--r-- | Examples/README.md | 15 | ||||
-rw-r--r-- | include/value.h | 2 | ||||
-rwxr-xr-x | src/libphpcpp.so | bin | 137255 -> 137308 bytes | |||
-rw-r--r-- | src/value.cpp | 113 |
5 files changed, 139 insertions, 14 deletions
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) diff --git a/include/value.h b/include/value.h index 5763d67..336bb1d 100644 --- a/include/value.h +++ b/include/value.h @@ -290,7 +290,7 @@ public: bool isFloat() const { return type() == floatType; } bool isObject() const { return type() == objectType; } bool isArray() const { return type() == arrayType; } - bool isCallable() const { return type() == callableType; } + bool isCallable() const; /** * Retrieve the value as number diff --git a/src/libphpcpp.so b/src/libphpcpp.so Binary files differindex ed993df..43da453 100755 --- a/src/libphpcpp.so +++ b/src/libphpcpp.so diff --git a/src/value.cpp b/src/value.cpp index 603508e..69b35c1 100644 --- a/src/value.cpp +++ b/src/value.cpp @@ -672,6 +672,11 @@ Value Value::operator()() return exec(0, NULL); } +/** + * Call the function - if the variable holds a callable thing + * @param p0 The first parameter + * @return Value + */ Value Value::operator()(Value p0) { // array of parameters @@ -681,6 +686,12 @@ Value Value::operator()(Value p0) return exec(1, params); } +/** + * Call the function - if the variable holds a callable thing + * @param p0 The first parameter + * @param p1 The second parameter + * @return Value + */ Value Value::operator()(Value p0, Value p1) { // array of parameters @@ -690,6 +701,13 @@ Value Value::operator()(Value p0, Value p1) return exec(2, params); } +/** + * Call the function - if the variable holds a callable thing + * @param p0 The first parameter + * @param p1 The second parameter + * @param p2 The third parameter + * @return Value + */ Value Value::operator()(Value p0, Value p1, Value p2) { // array of parameters @@ -699,6 +717,14 @@ Value Value::operator()(Value p0, Value p1, Value p2) return exec(3, params); } +/** + * Call the function - if the variable holds a callable thing + * @param p0 The first parameter + * @param p1 The second parameter + * @param p2 The third parameter + * @param p3 The fourth parameter + * @return Value + */ Value Value::operator()(Value p0, Value p1, Value p2, Value p3) { // array of parameters @@ -708,6 +734,15 @@ Value Value::operator()(Value p0, Value p1, Value p2, Value p3) return exec(4, params); } +/** + * Call the function - if the variable holds a callable thing + * @param p0 The first parameter + * @param p1 The second parameter + * @param p2 The third parameter + * @param p3 The fourth parameter + * @param p4 The fifth parameter + * @return Value + */ Value Value::operator()(Value p0, Value p1, Value p2, Value p3, Value p4) { // array of parameters @@ -717,6 +752,16 @@ Value Value::operator()(Value p0, Value p1, Value p2, Value p3, Value p4) return exec(5, params); } +/** + * Call the function - if the variable holds a callable thing + * @param p0 The first parameter + * @param p1 The second parameter + * @param p2 The third parameter + * @param p3 The fourth parameter + * @param p4 The fifth parameter + * @param p5 The sixth parameter + * @return Value + */ Value Value::operator()(Value p0, Value p1, Value p2, Value p3, Value p4, Value p5) { // array of parameters @@ -726,6 +771,17 @@ Value Value::operator()(Value p0, Value p1, Value p2, Value p3, Value p4, Value return exec(6, params); } +/** + * Call the function - if the variable holds a callable thing + * @param p0 The first parameter + * @param p1 The second parameter + * @param p2 The third parameter + * @param p3 The fourth parameter + * @param p4 The fifth parameter + * @param p5 The sixth parameter + * @param p6 The seventh parameter + * @return Value + */ Value Value::operator()(Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6) { // array of parameters @@ -735,6 +791,18 @@ Value Value::operator()(Value p0, Value p1, Value p2, Value p3, Value p4, Value return exec(7, params); } +/** + * Call the function - if the variable holds a callable thing + * @param p0 The first parameter + * @param p1 The second parameter + * @param p2 The third parameter + * @param p3 The fourth parameter + * @param p4 The fifth parameter + * @param p5 The sixth parameter + * @param p6 The seventh parameter + * @param p7 The eighth parameter + * @return Value + */ Value Value::operator()(Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7) { // array of parameters @@ -744,6 +812,19 @@ Value Value::operator()(Value p0, Value p1, Value p2, Value p3, Value p4, Value return exec(8, params); } +/** + * Call the function - if the variable holds a callable thing + * @param p0 The first parameter + * @param p1 The second parameter + * @param p2 The third parameter + * @param p3 The fourth parameter + * @param p4 The fifth parameter + * @param p5 The sixth parameter + * @param p6 The seventh parameter + * @param p7 The eighth parameter + * @param p8 The ninth parameter + * @return Value + */ Value Value::operator()(Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7, Value p8) { // array of parameters @@ -753,6 +834,20 @@ Value Value::operator()(Value p0, Value p1, Value p2, Value p3, Value p4, Value return exec(9, params); } +/** + * Call the function - if the variable holds a callable thing + * @param p0 The first parameter + * @param p1 The second parameter + * @param p2 The third parameter + * @param p3 The fourth parameter + * @param p4 The fifth parameter + * @param p5 The sixth parameter + * @param p6 The seventh parameter + * @param p7 The eighth parameter + * @param p8 The ninth parameter + * @param p9 The tenth parameter + * @return Value + */ Value Value::operator()(Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7, Value p8, Value p9) { // array of parameters @@ -789,14 +884,6 @@ Value Value::exec(int argc, zval ***params) */ Type Value::type() const { - // When the type should be a callable Z_TYPE_P returns objectType - // To circumvent this, we check whether _val is callable - char *func_name; - if(zend_is_callable(_val, 0, &func_name)) - { - return callableType; - } - // return regular type return (Type)Z_TYPE_P(_val); } @@ -830,6 +917,16 @@ Value &Value::setType(Type type) } /** + * Check if the variable holds something that is callable + * @return bool + */ +bool Value::isCallable() const +{ + // we can not rely on the type, because strings can be callable as well + return zend_is_callable(_val, 0, NULL); +} + +/** * Make a clone of the type * @return Value */ |