常数

最后更新于:2022-04-02 02:33:06

[TOC] ## 示例 ### 全局常量 ``` #include 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; } } ``` ``` 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.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()
main.cpp ``` #include 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; } } ```

main.php ```
';