即将在下月底发布的 PHP 7.4 将会引入一个有趣的新功能,那就是期待已久的对 FFI(外部函数接口,Foreign Function Interface)的支持。此功能使得开发者可以在原生 PHP 代码中调用 C 定义的函数/变量/数据结构。 下面摘录一个简单的例子: Redis 客户端 目前存在使用 C 或 PHP 编写的 Redis 客户端的各种实现,此示例演示了使用 FFI 来访问 hiredis 库的函数。
输出摘要 $ php74 -d ffi.preload=preload-redis.h -d opcache.preload=preload-redis.inc redis.php ... + Remi\Redis::__construct(localhost, 6379) + Remi\Redis::initFFI() + Remi\Redis::del(foo) int(1) + Remi\Redis::get(foo) NULL + Remi\Redis::set(foo, 2019/10/23 12:45:03) string(2) "OK" + Remi\Redis::get(foo) string(19) "2019/10/23 12:45:03" + Remi\Redis::__destruct 如果希望了解有关 PHP 7.4 FFI 的更多信息,可以查看 PHP.net 上的文档及其基本示例。在发布 PHP 7.4 GA 前估计会经历多个 RC 版本,GA 预计将在 11 月 28 日左右发布。 |