使用静态方法的PHP C ++扩展

时间:2012-06-18 06:56:09

标签: php static-methods php-extension

我有一个包含静态方法的C ++类。

宣言:

class QConfig
{       
    public:
        static int testStatic();
};

实现:

int QConfig::testStatic()
{
    return 12345;
}

我希望能够在PHP中使用它,如下所示:

echo QConfig::testStatic() // expected result: 12345

我尝试过使用:

PHP_METHOD(QConfig, testStatic)
{
    QConfig *qconfig;
    qconfig_object *obj = (qconfig_object *)zend_object_store_get_object(
        getThis() TSRMLS_CC);
    qconfig = obj->qconfig;
    if (qconfig != NULL) {
        RETURN_LONG(qconfig->testStatic, 1);
    }
    RETURN_NULL();
}

...

function_entry qconfig_methods[] = {
    PHP_ME(QConfig,  staticTest,      NULL, ZEND_ACC_PUBLIC)
    {NULL, NULL, NULL}
};

但这会导致构建错误:

/home/testuser/test/mytest.cc:81:9: error: invalid conversion from 'int (*)()' to 'long int' [-fpermissive]
/home/testuser/test/mytest.cc: At global scope:
/home/testuser/test/mytest.cc:122:1: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
make: *** [mytest.lo] Error 1

如何在C ++ PHP扩展中访问静态方法?

1 个答案:

答案 0 :(得分:1)

ZEND_ACC_PUBLIC更改为ZEND_ACC_STATIC