1

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?

1
  • Typo above: should be "RETURN_LONG", not "RETURN_INT" Commented Jun 18, 2012 at 7:01

1 Answer 1

1

Change ZEND_ACC_PUBLIC to ZEND_ACC_STATIC.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.