企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] ## 示例 ### 普通成员变量 <details> <summary>main.cpp</summary> ``` #include <phpcpp.h> class Example : public Php::Base { public: Example() = default; virtual ~Example() = default; void __construct() { Php::Value self(this); } }; extern "C" { PHPCPP_EXPORT void *get_module() { static Php::Extension myExtension("my_extension", "1.0"); Php::Class<Example> example("Example"); example.method<&Example::__construct>("__construct"); // 设置元素值 example.property("property1", "xyz", Php::Public); myExtension.add(std::move(example)); return myExtension; } } ``` </details> <br /> <details> <summary>main.php</summary> ``` <?php $e = new Example(); // 已有元素值 var_dump($e->property1); $e->property1 = "new value1"; var_dump($e->property1); // new value1 ``` </details> <br /> ### 静态属性和类常量 <details> <summary>main.cpp</summary> ``` #include <phpcpp.h> class Example : public Php::Base { public: Example()=default; virtual ~Example()=default; }; extern "C" { PHPCPP_EXPORT void *get_module() { static Php::Extension myExtension("my_extension", "1.0"); Php::Class<Example> example("Example"); // the Example class has a class constant example.property("MY_CONSTANT", "some value", Php::Const); // and a public static propertie example.property("my_property", "initial value", Php::Const | Php::Static); // add the class to the extension myExtension.add(std::move(example)); // return the extension return myExtension; } } ``` </details> <br /> <details> <summary>main.php</summary> ``` <?php var_dump(Example::MY_CONSTANT); // 无法实现 ,出现错误 var_dump(Example::$my_property); ``` </details> <br /> ### 智能属性 <details> <summary>main.cpp</summary> ``` #include <phpcpp.h> class Example : public Php::Base { private: int _value = 0; public: Example() = default; virtual ~Example() = default; Php::Value getValue() const { return _value; } void setValue(const Php::Value &value) { _value = value; if (_value > 100) _value = 100; } }; extern "C" { PHPCPP_EXPORT void *get_module() { static Php::Extension myExtension("my_extension", "1.0"); Php::Class<Example> example("Example"); // 对属性进行 读取与设置 example.property("value", &Example::getValue, &Example::setValue); myExtension.add(std::move(example)); return myExtension; } } ``` </details> <br /> <details> <summary>main.php</summary> ``` <?php $object = new Example(); $object->value="23"; var_dump($object->value); // 23 ``` </details> <br />