I learning php extension write in C.
Now i meet code below, i want to declare a Closure function.
How to achieve this in a PHP extension?
class myClass
{
public function removeListener($event, $listener)
{
// some code
}
public function once($event, callable $listener)
{
// How to declare the closure and contain the use grammar features?
$onceListener = function () use (&$onceListener, $event, $listener) {
$this->removeListener($event, $onceListener);
call_user_func_array($listener, func_get_args());
};
$this->on($event, $onceListener);
}
public function on($event, $listener)
{
// some code
}
}
@NikiC Thanks for your comment.
I'm try use the zend_create_closure
API below.
static void once_listener_handler(INTERNAL_FUNCTION_PARAMETERS)
{
// How to get variables from syntax `use(&$onceListener, $event, $listener)` here.
// And use these variables like `php_var_dump(zval);`.
RETURN_TRUE;
}
zend_function mptr;
mptr.type = ZEND_USER_FUNCTION;
mptr.common.arg_info = NULL;
mptr.common.num_args = 0;
mptr.common.required_num_args = 0;
mptr.common.prototype = NULL;
mptr.common.scope = NULL;
mptr.internal_function.handler = once_listener_handler;
zend_create_closure (once_closure, &mptr, NULL, NULL TSRMLS_CC);
I got error below.
/mac/sourcecode/php-5.5.23/Zend/zend_hash.c(1055) : ht=0x9f8c69 is inconsistent
/mac/sourcecode/php-5.5.23/Zend/zend_hash.c(551) : ht=0x7ffff7e0e6c8 is inconsistent
Program received signal SIGSEGV, Segmentation fault.
0x00000000009f7740 in zend_mm_check_ptr (heap=0x1333ee0, ptr=0x7ffff7e12150, silent=1,
__zend_filename=0xfa7b38 "/mac/sourcecode/php-5.5.23/Zend/zend_hash.c", __zend_lineno=568,
__zend_orig_filename=0x0, __zend_orig_lineno=0) at /mac/sourcecode/php-5.5.23/Zend/zend_alloc.c:1384
1384 if (p->info._size != ZEND_MM_NEXT_BLOCK(p)->info._prev) {
Can your show some code snippet for zend_create_closure
?
And try to use some variables in internal_function.handler
callback that pass by call place context.
Thank your :)