1

我试图通过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 &params) {
    std::string key = params[0];
    ...
    ...
    return Php::Object(...);
}

// PHP: $a = new BDict(); $b = new BDict(); $a->addItem($b);
void addItem(Php::Parameters &params) {
    std::string key = params[0];

    /**
     * Here's the part confusing me
     * Is there something like:
     */
    BItem *toInsert = &params[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 扩展的新手,任何帮助或提示将不胜感激。

4

1 回答 1

0

根据埃米尔的回答

void myFunction(Php::Parameters &params)
{
    // 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
}
于 2015-09-21T05:14:20.697 回答