💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[TOC] ## 示例 ### hello word <details> <summary>main.cpp</summary> ``` #include <phpcpp.h> Php::Value myDiv(Php::Parameters &params) { if (params[1] == 0) throw Php::Exception("Division by zero"); return params[0] / params[1]; } extern "C" { PHPCPP_EXPORT void *get_module() { static Php::Extension extension("my_extension", "1.0"); extension.add<myDiv>("myDiv", { Php::ByVal("a", Php::Type::Numeric, true), Php::ByVal("b", Php::Type::Numeric, true) }); return extension; } } ``` </details> <br /> <details> <summary>main.php</summary> ``` <?php try { echo (myDiv(5, 0) . "\n"); } catch (Exception $exception) { echo ($exception->getMessage()); // Division by zer } ``` </details> <br /> ### 捕获C ++中的异常 <details> <summary>main.cpp</summary> ``` #include <phpcpp.h> Php::Value callMe(Php::Parameters &params) { try { return params[0](); } catch (Php::Exception &exception) { return "Exception caught!\n"; } } extern "C" { PHPCPP_EXPORT void *get_module() { static Php::Extension extension("my_extension", "1.0"); extension.add<callMe>("callMe", { Php::ByVal("callback", Php::Type::Callable, true) }); return extension; } } ``` </details> <br /> <details> <summary>main.php</summary> ``` <?php $output = callMe(function() { return "First call"; }); echo("$output\n"); // First call $output = callMe(function() { throw new Exception("Sorry...\n"); return "Second call\n"; }); echo("$output\n"); // Exception caught! ``` </details> <br />