next up previous contents
Next: Passing Arguments Up: Usage Previous: Initializing   Contents

Calling functions

C++ is a strongly typed language so the interface with a weakly typed language like PHP requires that types be called out explicitly. Polymorphism in C++ requires that functions of the same name vary in argument signature (just changing the return type isn't enough). As a consequence there are a host of functions where the return type expected is specified in the function name such as call_void, call_bool, call_double_arr, and call_string_string_hash_map. Here is an example of usage:

long memused = p.call_long("memory_get_usage");
hash_set<string> ex;
ex = p.call_string_hash_set("get_loaded_extensions");

In the first call the PHP builtin function memory_get_usage is invoked which takes no arguments and returns the number of bytes currently allocated internally by the PHP interpreter. Observe that there are no parens in the function name. The return is given in a long, which is the default numeric type for PHP. NOTE: Any function calls that return bool, int, or unsigned int are really returning a long and the php object is just typecasting in C++.

The second call demonstrates that it is no more difficult to take advantage of the more complex return types. The PHP builtin get_loaded_extensions will return an array of the names of all the extensions loaded into the current build of PHP which is then converted into a hash_set. See the Function Reference section for a list of all the functions available.


next up previous contents
Next: Passing Arguments Up: Usage Previous: Initializing   Contents
Andrew Bosworth 2008-03-24