完整 PHP/FFI/preloading 示例

php.ini

ffi.enable=preload
opcache.preload=preload.php

preload.php

<?php
FFI
::load(__DIR__ . "/dummy.h");
opcache_compile_file(__DIR__ . "/dummy.php");
?>

dummy.h

#define FFI_SCOPE "DUMMY"
#define FFI_LIB "libc.so.6"
 
int printf(const char *format, ...);

dummy.php

<?php
final class Dummy {
private static
$ffi = null;
function
__construct() {
if (
is_null(self::$ffi)) {
self::$ffi = FFI::scope("DUMMY");
}
}
function
printf($format, ...$args) {
return (int)
self::$ffi->printf($format, ...$args);
}
}
?>

test.php

<?php
$d
= new Dummy();
$d->printf("Hello %s!\n", "world");
?>
add a note add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top