PHP 콜백

PHP 클로저를 함수 포인터 유형의 기본 변수에 할당하거나 함수 인수로 전달할 수 있습니다.

                  
<?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";
?>
                  
                

위의 예는 다음을 출력합니다.

Hello World 1!
{
        Hello World 2!
}
Hello World 3!
                

이것이 작동하더라도 이 기능은 모든 libffi 플랫폼에서 지원되지 않으며 효율적이지 않으며 요청이 끝날 때까지 리소스를 누출합니다.

따라서 PHP 콜백 사용을 최소화하는 것이 좋습니다.