php教程

A Complete PHP/FFI/preloading Example

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

User Contributed Notes

There are no user contributed notes for this page.

CopyRight © 2008-2022 verySource.Com All Rights reserved. 京ICP备17048824号-1 京公网安备:11010502034788