合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
[TOC] ## 示例 ### 继承可数 Countable 接口 Countable 接口 ``` Countable { /* 方法 */ abstract public count ( void ) : int } ``` <details> <summary>main.cpp</summary> ``` #include <phpcpp.h> class Counter : public Php::Base, public Php::Countable { private: int _value = 0; public: Counter() {} virtual ~Counter() {} Php::Value increment() { return ++_value; } Php::Value decrement() { return --_value; } virtual long count() override { return _value; } }; extern "C" { PHPCPP_EXPORT void *get_module() { static Php::Extension myExtension("my_extension", "1.0"); Php::Class<Counter> counter("Counter"); counter.method("increment", &Counter::increment); counter.method("decrement", &Counter::decrement); myExtension.add(std::move(counter)); // return the extension return myExtension; } } ``` </details> <br /> <details> <summary>main.php</summary> ``` <?php $counter = new Counter(); $counter->increment(); $counter->increment(); $counter->increment(); echo(count($counter)."\n"); // 3 ``` </details> <br /> ### ArrayAccess 接口 将对象行为转为数组 格式: ``` ArrayAccess { /* 方法 */ abstract public offsetExists ( mixed $offset ) : boolean abstract public offsetGet ( mixed $offset ) : mixed abstract public offsetSet ( mixed $offset , mixed $value ) : void abstract public offsetUnset ( mixed $offset ) : void } ``` <details> <summary>main.cpp</summary> ``` #include <phpcpp.h> class Map : public Php::Base, public Php::Countable, public Php::ArrayAccess { private: std::map<std::string, std::string> _map; public: Map() {} virtual ~Map() {} virtual long count() override { return _map.size(); } virtual bool offsetExists(const Php::Value &key) override { return _map.find(key) != _map.end(); } virtual void offsetSet(const Php::Value &key, const Php::Value &value) override { _map[key] = value.stringValue(); } virtual Php::Value offsetGet(const Php::Value &key) override { return _map[key]; } virtual void offsetUnset(const Php::Value &key) override { _map.erase(key); } }; extern "C" { PHPCPP_EXPORT void *get_module() { static Php::Extension myExtension("my_extension", "1.0"); Php::Class<Map> map("Map"); myExtension.add(std::move(map)); return myExtension; } } ``` </details> <br /> <details> <summary>main.php</summary> ``` <?php <?php $map = new Map(); $map["a"] = 1234; $map["b"] = "xyz"; $map["c"] = 0; unset($map['a']); var_dump($map["a"]) . PHP_EOL; // string: "" var_dump($map["b"]) . PHP_EOL; // string: "xyz" var_dump($map["c"]) . PHP_EOL; // string: "0" var_dump($map["d"]) . PHP_EOL; // string:"" //这会导致一个致命错误, // ArrayAccess方法没有导出到用户空间 echo ($map->offsetGet("a") . "\n"); ``` </details> <br /> ### foreach 进行迭代(测试失败) ### 可序列化的接口 <details> <summary>main.cpp</summary> ``` #include <phpcpp.h> class Counter : public Php::Base, public Php::Serializable { private: int _value = 0; public: Counter() = default; virtual ~Counter() = default; Php::Value increment() { return ++_value; } Php::Value decrement() { return --_value; } Php::Value value() const { return _value; } virtual std::string serialize() override { return std::to_string(_value); } virtual void unserialize(const char *buffer, size_t size) override { _value = std::atoi(buffer); } }; extern "C" { PHPCPP_EXPORT void *get_module() { static Php::Extension myExtension("my_extension", "1.0"); Php::Class<Counter> counter("Counter"); counter.method<&Counter::increment>("increment"); counter.method<&Counter::decrement>("decrement"); counter.method<&Counter::value>("value"); myExtension.add(std::move(counter)); return myExtension; } } ``` </details> <br /> <details> <summary>main.php</summary> ``` <?php // create an empty counter and increment it a few times $counter = new Counter(); $counter->increment(); $counter->increment(); // turn the counter into a storable string $serializedCounter = serialize($counter); // revive the counter back into an object $revivedCounter = unserialize($serializedCounter); // show the counter value echo($revivedCounter->value()."\n");//2 ?> ``` </details> <br />