在 PHP-CPP 中将 Php::Object 转换回 C++ 对象?
Convert a Php::Object back to a C++ object in PHP-CPP?
我试图制作一个 Bencode extension for PHP by PHP-CPP,所以有几个 类 比如:
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++ 部分,"convert" 将其传回 C++ 对象,最后将其插入 BData
?
我是 php 扩展的新手,非常感谢任何帮助或提示。
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
}
我试图制作一个 Bencode extension for PHP by PHP-CPP,所以有几个 类 比如:
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++ 部分,"convert" 将其传回 C++ 对象,最后将其插入 BData
?
我是 php 扩展的新手,非常感谢任何帮助或提示。
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
}