将Php::对象转换回C++;PHP-CPP中的对象?
我试图通过创建PHP扩展,因此有几个类,如:将Php::对象转换回C++;PHP-CPP中的对象?,php,c++,php-extension,php-cpp,Php,C++,Php Extension,Php Cpp,我试图通过创建PHP扩展,因此有几个类,如: 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 {
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:public Php::Base{
公众:
虚拟std::string getType()常量{
返回“BItem”;
}
};
BDict类:公共比特{
公众:
std::无序地图数据;
std::string getType()常量{
返回“BDict”;
}
Php::Value getItem(Php::Parameters和params){
std::string key=params[0];
...
...
返回Php::Object(…);
}
//PHP:$a=new BDict();$b=new BDict();$a->addItem($b);
void addItem(Php::Parameters和params){
std::string key=params[0];
/**
*这是让我困惑的部分
*是否有类似于:
*/
BItem*toInsert=¶ms[1];//但是,params[1]实际上是一个Php::对象
insert({key,toInsert});
}
};
类BStr:公共比特{…};
类BList:公共位{…};
类BInt:公共位{…};
除了BItem
之外的所有类型都可以插入到BDict
中
< > P>创建了其中一个实例后,如何将其传递回C++部分,将其“转换”为C++对象,并最终将其插入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
}