I am currently connecting a C library in PHP using FFI.
My current code works properly and I am now trying to call a PHP lambda function from my C code and I am stuck with the following error:
PHP Fatal error: Uncaught FFI\Exception: Cannot prepare callback CIF
The goal would be to pass the following PHP function:
fn($core, $state, $message) => var_dump($message);
As a parameter of the following C method
/**
* Set the #LinphoneCoreGlobalStateChangedCb callback.
* @param cbs A #LinphoneCoreCbs. @notnil
* @param cb The callback.
*/
LINPHONE_PUBLIC void linphone_core_cbs_set_global_state_changed(LinphoneCoreCbs *cbs, LinphoneCoreCbsGlobalStateChangedCb cb);
https://gitlab.linphone.org/BC/public/liblinphone/-/blob/master/include/linphone/core.h#L359
And here is the non-working code:
$ffi = FFI::cdef("
...
typedef struct LinphoneCore LinphoneCore;
typedef struct LinphoneGlobalState LinphoneGlobalState;
typedef void (*LinphoneCoreCbsGlobalStateChangedCb)(LinphoneCore *core, LinphoneGlobalState state, const char *message);
typedef LinphoneCoreCbsGlobalStateChangedCb LinphoneCoreGlobalStateChangedCb;
void linphone_core_cbs_set_global_state_changed(LinphoneCoreCbs *cbs, LinphoneCoreCbsGlobalStateChangedCb cb);
...
",
"./liblinphone.so");
$cbs = $ffi->linphone_factory_create_core_cbs($ffi->linphone_factory_get());
$func = fn($core, $state, $message) => var_dump($message);
$ffi->linphone_core_cbs_set_global_state_changed($cbs, $func);
It seems that I can directly assign lambda function to PHP FFI code like that, as stated in the documentation https://www.php.net/manual/en/ffi.examples-callback.php but I'm maybe missing something.
Thanks in advance for the help.