我有一个包含静态方法的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 扩展中访问静态方法?
将ZEND_ACC_PUBLIC
更改为ZEND_ACC_STATIC
。