9

I am writing a PHP extension. From the C code I try to invoke a static method in PHP code.

The PHP-method looks like this:

<?php
class Model {
  static method GetModelById($id) { ... }
}
?>

The call in C looks like this:

if( call_user_function_ex(
      &((*ce)->function_table),
      NULL, &fname, &retval_ptr,
      1, func_params, 0, NULL TSRMLS_CC
    ) == SUCCESS
){
  // do some stuff here ...
}

... where all passed parameters should contain proper values. The strange thing here is: if I compile my extension against php 5.2 the code works fine, if I compile this against php 5.3, the method call fails with no error message.

I also tried zend_call_method with no success in either version.

Can anyone give a tip for me? How would you call a static method from C?

Thanks in advance!

Edit

Sorry guys, I got it working via zend_call_method like so:

if( zend_call_method( NULL, *ce, NULL, 
                     "getmodelbyid", 
                     strlen("getmodelbyid"), 
                     &retval_ptr, 1, p1, 
                     NULL TSRMLS_CC ) == FAILURE) {
    php_printf("gosh!");
} 
else {
    php_printf("yep!"); 
}

... so I learned:

  1. Function names must always be in lowercase
  2. You better have a look at PHP's source code when it comes to string lengths (zend_call_method adds +1 internally).

Although I am new to C, I think the PHP code base is over-compilcated in many ways!

Hope this helps someone else!

2
  • 5
    Peter, would you mind pasting this into the answer section, submitting and then marking the answer as accepted to indicate the question has been resolved? Commented Jan 31, 2012 at 23:45
  • 1
    Yes, it helps those of us digging through unanswered questions to know it's already been resolved. Commented Sep 30, 2012 at 2:53

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.