💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[TOC] ## 示例 ### 全局常量 ``` #include <phpcpp.h> extern "C" { PHPCPP_EXPORT void *get_module() { static Php::Extension myExtension("my_extension", "1.0"); // floating point constants myExtension.add(Php::Constant("MY_CONSTANT_3", 3.1415927)); // string constants myExtension.add(Php::Constant("MY_CONSTANT_5", "This is a constant value")); // null constants myExtension.add(Php::Constant("MY_CONSTANT_7", nullptr)); // return the extension return myExtension; } } ``` ``` <?php echo(MY_CONSTANT_3."\n"); echo(MY_CONSTANT_5."\n"); echo(MY_CONSTANT_7."\n"); ``` ### 类级别常量 ``` #include <phpcpp.h> class Dummy : public Php::Base { }; extern "C" { PHPCPP_EXPORT void *get_module() { static Php::Extension myExtension("my_extension", "1.0"); Php::Class<Dummy> dummy("Dummy"); // 有三方方式给类赋值常量, dummy.property("MY_CONSTANT_1", 1, Php::Const); dummy.property("MY_CONSTANT_2", "abcd", Php::Const); dummy.constant("MY_CONSTANT_3", "xyz"); dummy.constant("MY_CONSTANT_4", 3.1415); dummy.add(Php::Constant("MY_CONSTANT_5", "constant string")); dummy.add(Php::Constant("MY_CONSTANT_5", true)); // add the class to the extension myExtension.add(std::move(dummy)); // return the extension return myExtension; } } ``` ### 运行时常量 在运行时从C ++代码中查找用户空间常量的值,或者要查找是否定义了常量,则可以简单地使用Php :: constant()或Php :: define() <details> <summary>main.cpp</summary> ``` #include <phpcpp.h> void example_function() { //检查是否定义了某个用户空间常数 if (Php::defined("USER_SPACE_CONSTANT")) { //查找一个常量的值 Php::Value constant = Php::constant("ANOTHER_CONSTANT"); //在运行时定义其他常量 Php::define("DYNAMIC_CONSTANT", 12345); } } extern "C" { PHPCPP_EXPORT void *get_module() { static Php::Extension myExtension("my_extension", "1.0"); myExtension.add("example_function", example_function); return myExtension; } } ``` </details> <br /> <details> <summary>main.php</summary> ``` <?php define('USER_SPACE_CONSTANT',1); example_function(); echo DYNAMIC_CONSTANT; //12345 ``` </details> <br />