基础SPL接口
最后更新于:2022-04-02 02:33:27
[TOC]
## 示例
### 继承可数 Countable 接口
Countable 接口
```
Countable {
/* 方法 */
abstract public count ( void ) : int
}
```
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;
}
}
```
### 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 } ```
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
### foreach 进行迭代(测试失败) ### 可序列化的接口
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.cpp
``` #includemain.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
``` #includemain.php
``` offsetGet("a") . "\n"); ```### foreach 进行迭代(测试失败) ### 可序列化的接口