基础SPL接口

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

[TOC] ## 示例 ### 继承可数 Countable 接口 Countable 接口 ``` Countable { /* 方法 */ abstract public count ( void ) : int } ```
main.cpp ``` #include 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.method("increment", &Counter::increment); counter.method("decrement", &Counter::decrement); myExtension.add(std::move(counter)); // return the extension return myExtension; } } ```

main.php ``` increment(); $counter->increment(); $counter->increment(); echo(count($counter)."\n"); // 3 ```

### 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 } ```
main.cpp ``` #include class Map : public Php::Base, public Php::Countable, public Php::ArrayAccess { private: std::map _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"); myExtension.add(std::move(map)); return myExtension; } } ```

main.php ``` offsetGet("a") . "\n"); ```

### foreach 进行迭代(测试失败) ### 可序列化的接口
main.cpp ``` #include 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.method<&Counter::increment>("increment"); counter.method<&Counter::decrement>("decrement"); counter.method<&Counter::value>("value"); myExtension.add(std::move(counter)); return myExtension; } } ```

main.php ``` 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 ?> ```

';