多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] ## 示例 ``` #include <phpcpp.h> #include <iostream> void example_function(Php::Parameters &params) { Php::Value array = params[0]; // 调用 php的 array_keys std::vector<std::string> keys = Php::array_keys(array); for (auto &key : keys) { Php::out << "key: " << key << std::endl; } // call a function from user space Php::Value data = Php::call("some_function", "some_parameter"); Php::out<<"输出 some_function 的参数"<<data << std::flush; // 相当于 $time = new DateTime('now') Php::Object time("DateTime", "now"); // 调用 time.format Php::out << time.call("format", "Y-m-d H:i:s") << std::endl; // 获得回调函数 Php::Value callback = params[1]; // 调用函数 callback("some","parameter"); // in PHP it is possible to create an array with two parameters, the first // parameter being an object, and the second parameter should be the name // of the method, we can do that in PHP-CPP too Php::Array time_format({time, "format"}); // call the method that is stored in the array Php::out << time_format("Y-m-d") << std::endl; } extern "C" { PHPCPP_EXPORT void *get_module() { static Php::Extension extension("my_extension", "1.0"); extension.add("example_function", example_function); return extension; } } ``` index.php ``` // define a user space function function some_function($param) { echo("some_function 的参数 $param\n"); } // example input $input = array( 'x' => 10, 'y' => 20, 'z' => 30 ); example_function($input, function($param1, $param2) { echo("匿名函数的参数 $param1 $param2\n"); }); //key: x // key: y // key: z // some_function 的参数 some_parameter // 输出 some_function 的参数2020-09-02 18:26:52 // 匿名函数的参数 some parameter // 2020-09-02 ``` > 如果调用不存在的函数或方法,将抛出Php :: Exception。如果您没有在C ++代码中捕获此异常,则它将冒泡并出现在PHP用户空间中。