PHP 7.4版本(即将发行正式版,当前时间:2019年10月31日09:36:31)已经出了 FFI(Foreign Function Interface:外部函数接口),也就是说以后你可以不用写 C 扩展了,可以直接调用编译好的 so 库,不过该扩展当前状态为实验性的,稳定性有待验证。官方示例如下:
<?php
// create gettimeofday() binding
$ffi = FFI::cdef("
typedef unsigned int time_t;
typedef unsigned int suseconds_t;
struct timeval {
time_t tv_sec;
suseconds_t tv_usec;
};
struct timezone {
int tz_minuteswest;
int tz_dsttime;
};
int gettimeofday(struct timeval *tv, struct timezone *tz);
", "libc.so.6");
// create C data structures
$tv = $ffi->new("struct timeval");
$tz = $ffi->new("struct timezone");
// call C's gettimeofday()
var_dump($ffi->gettimeofday(FFI::addr($tv), FFI::addr($tz)));
// access field of C data structure
var_dump($tv->tv_sec);
// print the whole C data structure
var_dump($tz);
?>
PHP 7.4版本(即将发行正式版,当前时间:2019年10月31日09:36:31)已经出了 FFI(Foreign Function Interface:外部函数接口),也就是说以后你可以不用写 C 扩展了,可以直接调用编译好的 so 库,不过该扩展当前状态为
实验性
的,稳定性有待验证。官方示例如下:参考资料