在Php - cpp中将Php::Object转换回c++对象
Convert a Php::Object back to a C++ object in PHP-CPP?
我试图通过PHP- cpp为PHP做一个Bencode扩展,所以有几个类,如:
class BItem : public Php::Base {
public:
virtual std::string getType() const {
return "BItem";
}
};
class BDict : public BItem {
public:
std::unordered_map<std::string, BItem*> BData;
std::string getType() const {
return "BDict";
}
Php::Value getItem(Php::Parameters ¶ms) {
std::string key = params[0];
...
...
return Php::Object(...);
}
// PHP: $a = new BDict(); $b = new BDict(); $a->addItem($b);
void addItem(Php::Parameters ¶ms) {
std::string key = params[0];
/**
* Here's the part confusing me
* Is there something like:
*/
BItem *toInsert = ¶ms[1]; // However, params[1] is actually a Php::Object
BData.insert({key, toInsert});
}
};
class BStr : public BItem {...};
class BList : public BItem {...};
class BInt : public BItem {...};
除BItem
外的所有类型都可以插入BDict
。
因此,在创建其中一个实例后,我如何将其传递回c++部分,"转换"回c++对象并最终将其插入BData
?
我是php扩展的新手,任何帮助或提示都将非常感谢。
根据Emiel的回答
void myFunction(Php::Parameters ¶ms)
{
// store the first parameter in a variable
Php::Value object = params[0];
// we want to be 100% sure that the passed in parameter is indeed one of our own objects
if (!object.instanceOf("MySpecialClass")) throw Php::Exception("Wrong parameter passed");
// cast the PHP object back into a C++ class
MySpecialClass *cppobject = (MySpecialClass *)object.implementation();
// @todo add your own code
}
相关文章:
- 将加密消息从 php 发送到 C++ 应用程序,然后使用 CryptoPP 进行解密
- PHP Profiler语言 - 使用 C++ 从 PHP 扩展获取客户端 IP 和 URL
- Nginx RTMP与InstagramLive-PHP不起作用
- 如何使用PHP将上传到本地主机的一个html文件重定向到磁盘上的另一个html文档
- PHP & C++ $_FILES 和 HttpSendRequestA
- 如何使用 PHP-CPP 在 PHP 类中添加静态数组
- 如何在PHPCPP库中将C++map<string,map<string>>转换为php::value?
- 为什么用C++编写的 phpcpp 扩展函数比用 PHP 编写的函数慢
- QT:AES-256-CBC 根据 PHP 代码在C++中加密/解密
- 从 PHP 调用已编译的C++可执行文件来创建套接字
- 如何在 ubuntu 上的 php 脚本中获取程序(c,c++,java,python,php)的执行时间和内存使用量?
- 文件写入在PHP扩展c / c ++(linux)中不起作用
- 无论如何可以使用 C++/C 甚至 python 和 php
- 当 PHP、C++ 和 shell 脚本尝试访问同一个文件时,如何避免错误?
- PHP 中的实时仪表板,可从C++共享数据
- PHP / C++:共享内存时出现shm_open()错误
- 在PHP扩展中使用emalloc从线程时出现隔离错误
- 生成可以在PHP中加密的密钥,该密钥可以直接放置在C 代码中以进行解密
- 如何将JSON发送到PHP脚本并使用QT获得答案
- 当我使用 system() 在 PHP 中运行命令时没有LD_LIBRARY_PATH