Andy Turfer
Andy Turfer

Reputation: 201

PHP C++ Extension with a Static Method

I have a C++ class containing a static method.

The declaration:

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

Implementation:

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

I want to be able to use this from PHP as follows:

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

I've tried using:

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}
};

but this results in build errors:

/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

How to access a static method in a C++ PHP extension?

Upvotes: 1

Views: 338

Answers (1)

Vanessa Deagan
Vanessa Deagan

Reputation: 427

Change ZEND_ACC_PUBLIC to ZEND_ACC_STATIC.

Upvotes: 1

Related Questions