diff options
-rw-r--r-- | documentation/calling-functions-and-methods.html | 25 | ||||
-rw-r--r-- | include/array.h | 6 | ||||
-rw-r--r-- | include/classbase.h | 2 | ||||
-rw-r--r-- | include/value.h | 29 |
4 files changed, 48 insertions, 14 deletions
diff --git a/documentation/calling-functions-and-methods.html b/documentation/calling-functions-and-methods.html index 15e32f7..da3b57c 100644 --- a/documentation/calling-functions-and-methods.html +++ b/documentation/calling-functions-and-methods.html @@ -27,7 +27,7 @@ void example_function(Php::Parameters &params) { // first parameter is an array - Php::Value array == params[0]; + Php::Value array = params[0]; // call the PHP array_keys() function to get the parameter keys std::vector<std::string> keys = Php::array_keys(array); @@ -40,7 +40,7 @@ void example_function(Php::Parameters &params) } // call a function from user space - Php::Value data = Php::call("some_method", "some_parameter"); + Php::Value data = Php::call("some_function", "some_parameter"); // create an object (this will also call __construct()) Php::Object time("DateTime", "now"); @@ -87,7 +87,7 @@ extern "C" { }</code></pre> </p> <p> - In above example you can see quite some different ways how to call PHP + In above example you can see quite some different ways to call PHP functions from C++. The first one is the call to Php::array_keys(). The PHP-CPP internally has a long list of all important PHP functions, and you can call these functions directly from your extension. Php::array_keys() @@ -105,19 +105,18 @@ extern "C" { can. User space functions, or functions from optional PHP extensions are not automatically forwarded by the PHP-CPP library. Such functions can still be called by using the Php::call() function. You must supply the name of the - fuinction to call, and a list of optional arguments to call a function - from user space. + function to call, and an optional list of arguments. </p> <p> The Php::Object class (which is derived from Php::Value) can be used to create objects, and implicitly call the __construct() method. To call a - method on an object, you can use the method Php::Value::call() method, which + method on an object, you can use the Php::Value::call() method, which is used in the example to call the PHP method DateTime::format(). </p> <p> In PHP scripts you can create an array with two members: and object and - the name of a method. This array then automatically becomes callable. You - can do similar things in C++ as well, as we showed in the example with the + the name of a method. This array can then be used as if it was a regular + function. You can do similar things in C++, as we showed in the example with the "time_format" variable. </p> <p> @@ -126,6 +125,12 @@ extern "C" { <p> <pre class="language-php"><code> <?php + // define a user space function + function some_function($param) + { + echo("userspace function called with $param\n"); + } + // example input $input = array( 'x' => 10, @@ -133,8 +138,8 @@ extern "C" { 'z' => 30 ); - example_function($input, function($param) { - echo("callback called with param $param\n"); + example_function($input, function($param1, $param2) { + echo("lambda function called with param $param1 $param2\n"); }); ?></code></pre> </p> diff --git a/include/array.h b/include/array.h index 3b92826..dcd5e5f 100644 --- a/include/array.h +++ b/include/array.h @@ -59,6 +59,12 @@ public: Array(const std::map<std::string,T> &value) : Value(value) {} /** + * Constructor from an initializer list + * @param value + */ + Array(const std::initializer_list<Value> &value) : Value(value) {} + + /** * Destructor */ virtual ~Array() {} diff --git a/include/classbase.h b/include/classbase.h index 05ab2f4..25d081f 100644 --- a/include/classbase.h +++ b/include/classbase.h @@ -240,7 +240,7 @@ private: * @param count * @return int */ - static int countElements(zval *object, long *count TSRMLS_DC); + static int countElements(struct _zval *object, long *count); /** * Retrieve pointer to our own object handlers diff --git a/include/value.h b/include/value.h index 5800c6a..72bd895 100644 --- a/include/value.h +++ b/include/value.h @@ -72,8 +72,25 @@ public: template <typename T> Value(const std::vector<T> &input) : Value(Type::Array) { + // index + int i = 0; + + // set all elements + for (auto &elem : input) setRaw(i++, elem); + } + + /** + * Constructor from an initializer list + * @param value + */ + template <typename T> + Value(const std::initializer_list<T> &value) : Value(Type::Array) + { + // index + int i = 0; + // set all elements - for (size_t i=0; i<input.size(); i++) setRaw(i, input[i]); + for (auto &elem : value) setRaw(i++, elem); } /** @@ -395,8 +412,14 @@ public: // and fill the result vector for (size_t i = 0; i<count; i++) { - // check if the index exists, then add it - if (contains(i)) result.push_back((T)get(i)); + // check if the index exists + if (!contains(i)) continue; + + // get the value object + Value value(get(i)); + + // add it to the vector + result.push_back(value); } // done |