PHP7.4の新仕様 FFI,Foreign Function Interface について。Python的な…。
PHP7の時点で既にバイトコードになっていてPHP8からJITによるネイティブ化だったような。
PHP5.xから7.0で劇的に速くなった記憶があり、速度を追求するような機運があるのは確か。
投票システムの不備で”賛成24、反対15,過半数越えだから採択”みたいになり、
その後過半数から2/3に更新された、という経緯から、望まれない仕様なのは確かそう。
WordPressが速くなるのは嬉しいけども不毛すぎ..。
脱PHPが必要。
FFI is one of the features that made Python and LuaJIT very useful for fast prototyping. It allows calling C functions and using C data types from pure scripting language and therefore develop “system code” more productively. For PHP, FFI opens a way to write PHP extensions and bindings to C libraries in pure PHP.
<?php
/ create FFI object, loading libc and exporting function printf()
$ffi = FFI::cdef(
"int printf(const char *format, ...);", // this is regular C declaration
"libc.so.6");
// call C printf()
$ffi->printf("Hello %s!\n", "world");
// 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");
// calls C 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);