How to call C function in PHP

autoload
Release: 2023-03-09 10:28:02
Original
2516 people have browsed it

How to call C function in PHP

FFIprovides high-level languages to directly call each other, and forPHP,FFIallows us to Conveniently call various libraries written inClanguage. In fact, a large number of existing PHP extensions are packages of some existing C libraries, such as the commonly usedmysqli,curl,gettext, etc.,PECL There are also a large number of similar extensions in. Traditionally, when we need to use the capabilities of some existingClanguage libraries, we need to writewrapperin C language and package them into extensions. In this process Everyone needs to learn how to write the extension ofPHP. Of course, there are some convenient ways now, such asZephir., but there is still some learning cost, and withFFIIn the future, we can directly call the functions in the library written inClanguage in thePHPscript. In the decades of history of theClanguage, a large number of excellent libraries have been accumulated, andFFIdirectly allows us to conveniently enjoy this huge resource.

Note:FFIshould be used afterPHP7.4version

// 创建一个 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
Copy after login

Created two objects and calledC## respectively # Theprintf()andpow()functions.FFI::cdef()is used to create aFFIobject, which receives two parameters, one containing the regularC 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.dllor.sofile we need, which corresponds to our declaration string. For example, it does not exist inlibc.so.6pow()This kind of calculation function, so we have to find themathrelatedClanguage 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),内存大小
Copy after login
Use the

FFI::new()function to create aCdata structure, that is, variable declaration, the contents of these variables will be stored in thecdataattribute. The array can directly operate on the return value of this function. Of course, when we want to end use, we still need to useFFI::free()to release the variables, just likeClanguage 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!

Related labels:
c php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!