Home > Article > Backend Development > How to call C function in PHP
FFI
provides high-level languages to directly call each other, and for PHP
, FFI
allows us to Conveniently call various libraries written in C
language. In fact, a large number of existing PHP extensions are packages of some existing C libraries, such as the commonly used mysqli
, curl
, gettext
, etc., PECL There are also a large number of similar extensions in
. Traditionally, when we need to use the capabilities of some existing C
language libraries, we need to write wrapper
in C language and package them into extensions. In this process Everyone needs to learn how to write the extension of PHP
. Of course, there are some convenient ways now, such as Zephir.
, but there is still some learning cost, and with FFI
In the future, we can directly call the functions in the library written in C
language in the PHP
script. In the decades of history of the C
language, a large number of excellent libraries have been accumulated, and FFI
directly allows us to conveniently enjoy this huge resource.
Note: FFI
should be used after PHP7.4
version
// 创建一个 FFI 对象,加载 libc 并且导入 printf 函数 $ffi_printf = FFI::cdef( "int printf(const char *format, ...);", // C 的定义规则 "libc.so.6"); // 指定 libc 库 // 调用 C 的 printf 函数 $ffi_printf->printf("Hello %s!\n", "world"); // Hello World // 加载 math 并且导入 pow 函数 $ffi_pow = FFI::cdef( "double pow(double x, double y);", "libboost_math_c99.so.1.66.0"); // 这里调用的是 C 的 pow 函数,不是 PHP 自己的 echo $ffi_pow->pow(2,3), PHP_EOL; // 8
Created two objects and called C## respectively # The
printf() and
pow() functions.
FFI::cdef() is used to create a
FFI object, which receives two parameters, one containing the regular
C language (type, structure, function , variables, etc.) string that declares the sequence. Actually, this string can be copy-pasted from the C header file. The other parameter is the name of the shared library file to be loaded and linked. That is the
.dll or
.so file we need, which corresponds to our declaration string. For example, it does not exist in
libc.so.6
pow() This kind of calculation function, so we have to find the
math related
C language calculation function library.
Define variables and arrays
// 创建一个 int 变量 $x = FFI::new("int"); var_dump($x->cdata); // int(0) // 为变量赋值 $x->cdata = 5; var_dump($x->cdata); // int(5) // 计算变量 $x->cdata += 2; var_dump($x->cdata); // int(7) // 结合上面的两个 FFI 对象操作 echo "pow value:", $ffi_pow->pow($x->cdata, 3), PHP_EOL; // pow value:343 $ffi_printf->printf("Int Pow value is : %f\n", $ffi_pow->pow($x->cdata, 3)); // Int Pow value is : 343.000000 // 创建一个数组 $a = FFI::new("long[1024]"); // 为数组赋值 for ($i = 0; $i < count($a); $i++) { $a[$i] = $i; } var_dump($a[25]); // int(25) $sum = 0; foreach ($a as $n) { $sum += $n; } var_dump($sum); // int(523776) var_dump(count($a)); // int(1024) 数组长度 var_dump(FFI::sizeof($a)); // int(8192),内存大小Use the
FFI::new() function to create a
C data structure, that is, variable declaration, the contents of these variables will be stored in the
cdata attribute. The array can directly operate on the return value of this function. Of course, when we want to end use, we still need to use
FFI::free() to release the variables, just like
C language development.
Recommendation: 《2021 PHP interview questions summary (collection)》《php video tutorial》
The above is the detailed content of How to call C function in PHP. For more information, please follow other related articles on the PHP Chinese website!