array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'tr', ), 'this' => array ( 0 => 'ffi.examples-callback.php', 1 => 'PHP Callbacks', ), 'up' => array ( 0 => 'ffi.examples.php', 1 => 'Örnekler', ), 'prev' => array ( 0 => 'ffi.examples-basic.php', 1 => 'Basic FFI usage', ), 'next' => array ( 0 => 'ffi.examples-complete.php', 1 => 'A Complete PHP/FFI/preloading Example', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/ffi/examples.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
It is possible to assign a PHP closure to a native variable of function pointer type or to pass it as a function argument:
<?php
$zend = FFI::cdef("
typedef int (*zend_write_func_t)(const char *str, size_t str_length);
extern zend_write_func_t zend_write;
");
echo "Hello World 1!\n";
$orig_zend_write = clone $zend->zend_write;
$zend->zend_write = function($str, $len) {
global $orig_zend_write;
$orig_zend_write("{\n\t", 3);
$ret = $orig_zend_write($str, $len);
$orig_zend_write("}\n", 2);
return $ret;
};
echo "Hello World 2!\n";
$zend->zend_write = $orig_zend_write;
echo "Hello World 3!\n";
?>
Yukarıdaki örneğin çıktısı:
Hello World 1! { Hello World 2! } Hello World 3!
It is therefore recommended to minimize the usage of PHP callbacks.